重定向的input.txt文件的内容未显示在命令提示符中

时间:2015-02-20 06:50:54

标签: java cmd

我的程序正在运行。我的问题是当我从文件重定向输入时,输入没有显示在控制台中。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.List;
public class ReservationSystem{
public void printMenu() {
    System.out.print("Add [P]assenger, Add [G]roup, "
            + "[C]ancel Reservations, \nPrint Seating "
            + "[A]vailability Chart, Print [M]anifest, [Q]uit\n");
}   
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    ReservationSystem rs = new ReservationSystem();
    First fc = new First();
    Economy ec = new Economy();
    boolean done = false;
    do
    {
        String option = new String();
        rs.printMenu();
        option = in.nextLine();
        option = option.toUpperCase();

        while(!((option.equals("P")) || (option.equals("G"))||
           (option.equals("C")) || (option.equals("A"))||
           (option.equals("M")) || (option.equals("Q"))))
        {
            System.out.print("Please enter again:  ");
            option = in.nextLine();
            option = option.toUpperCase();
        }

        if(option.equals("P"))
        {
            String name = new String();
            String serviceClass = new String();
            String SeatPreference = new String();
            System.out.print("Name: ");
            name = in.nextLine();
            System.out.print("Service Class: ");
            serviceClass = in.nextLine();
            serviceClass = serviceClass.toUpperCase();
            System.out.print("Seat Preference: ");
            SeatPreference = in.nextLine();
            SeatPreference = SeatPreference.toUpperCase();
            if(serviceClass.equals("FIRST")) fc.addPassenger(name, SeatPreference);
            if(serviceClass.equals("ECONOMY")) ec.addPassenger(name, SeatPreference);
         }
        else if(option.equals("G"))
        {
            List<String> list = new ArrayList<String>();
            String groupName = new String(); 
            String name = new String(); 
            String serviceClass = new String();
            System.out.print("Group Name: ");
            groupName = in.nextLine();
            System.out.print("Names: ");
            name = in.nextLine();
            list = Arrays.asList(name.split(","));
            for(int i=0; i<list.size();i++)
            {
                list.set(i, list.get(i).trim());
            }
            System.out.print("Service Class: ");
            serviceClass = in.nextLine();
            serviceClass = serviceClass.toUpperCase();
            if(serviceClass.equals("FIRST")) fc.addGroup(list, groupName);
            if(serviceClass.equals("ECONOMY")) ec.addGroup(list, groupName);
        }
        else if(option.equals("C"))
        {
            String option1 = new String();
            System.out.print("Cancel [I]ndividual or [G]roup? ");
            option1 = in.nextLine();
            option1 = option1.toUpperCase();
            if(option1.equals("I"))
            {
                System.out.print("Name: ");
                String name = in.nextLine();
                fc.cancelIndividualReservation(name);
                ec.cancelIndividualReservation(name);
            }
            if(option.equals("G"))
            {
                System.out.print("Group Name: ");
                String groupName = in.nextLine();
                fc.cancelGroupReservation(groupName);
                ec.cancelGroupReservation(groupName);
            }
        }
        else if(option.equals("A"))
        {
            System.out.print("Service Class: ");
            String serviceClass = new String();
            serviceClass = in.nextLine();
            serviceClass = serviceClass.toUpperCase();
            if(serviceClass.equals("FIRST"))
            {
                fc.printAvailbilityList();
            }
            if(serviceClass.equals("ECONOMY"))
            {
                ec.printAvailbilityList();
            }
        }
        else if(option.equals("M"))
        {
            System.out.print("Service Class: ");
            String serviceClass = new String();
            serviceClass = in.nextLine();
            serviceClass = serviceClass.toUpperCase();
            if(serviceClass.equals("FIRST")){fc.printManifest();}
            if(serviceClass.equals("ECONOMY")){ec.printManifest();}
        }
        else{done = true;}  
    }while(done==false);
    in.close();
}

}

除了看到用户和程序之间的交互

Add [P]assenger, Add [G]roup, [C]ancel Reservations,
Print Seating [A]vailability Chart, Print [M]anifest, [Q]uit
P
Name: John Smith
Service Class: First
Seat Preference: W
Add [P]assenger, Add [G]roup, [C]ancel Reservations,
Print Seating [A]vailability Chart, Print [M]anifest, [Q]uit
Q

但是当我输入java Reservation < input.txt时,所有输入都没有显示出来。

Add [P]assenger, Add [G]roup, [C]ancel Reservations,
Print Seating [A]vailability Chart, Print [M]anifest, [Q]uit
Name: Service Class: Seat Preference: Add [P]assenger, Add [G]roup, [C]ancel
Reservations,  Print Seating [A]vailability Chart, Print [M]anifest, [Q]uit

1 个答案:

答案 0 :(得分:0)

请参阅http://pages.cs.wisc.edu/~cs302-2/resources/w13/TestArgs.txt

上的此示例

了解如何将文件名参数传递给扫描程序

public class TestArgs3 {
    public static void main( String [] args )
        throws FileNotFoundException
    {
        if ( args.length < 1 ) {
            System.out.println( usage() );
        }
        for ( int i=0; i < args.length; i++ ) {
            String filename = args[i];
            System.out.println("Scanning file " + filename + " ...");
            File file = new File( filename );
            Scanner filescanner = new Scanner( file );
            String line = filescanner.nextLine();
            System.out.println( line );
            filescanner.close();
        }
    }
    public static String usage() {
        return "Usage: java TestArgs filename1 filename2 filename3 ...";
    }
}