2013-03-31 60 views
0

因此,在此代码中,在方法“运行”中的类“运行”下,扫描器似乎不希望从第一次尝试接收输入,仅在第二秒线是否需要输入。我说第二行是因为我输入了输入,然后按回车两次,然后在THIRD行输入输入,它读取第二行,在这种情况下,它不会是什么。必须两次输入输入以供扫描器读取

我试过BufferedReader的结果相同,所以我相信我在做一些愚蠢的事情,并且忽略了某些事情。

import java.util.Scanner; 
import java.util.ArrayList; 
import java.util.Timer; 
import java.util.TimerTask; 

import java.io.*; 

class Global { 
    public static int stop = -1; 
} 

public class DataSort { 

    public static void main(String[] args){ 

     Timer timer = new Timer(); 
     Direct swit = new Direct(); 
     Run mprog = new Run(); 
     Help hlp = new Help(); 

     String newline = System.getProperty("line.separator"); 

     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     Scanner console = new Scanner(System.in); 

     System.out.println(newline); 
     System.out.println("Welcome to Data Sort! This Program is designed to sort information about targets discoverd by UAV and place the data in a table." + newline); 
     System.out.print("For help, press any key. To continue, please wait. "); 
     timer.schedule(swit, 3000); 

     try { 
      Global.stop = in.read(); 
     } 

     catch(IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      in.close(); 
     } 

     catch(IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

class Direct extends TimerTask { 

    public void run() { 

     Run mprog = new Run(); 
     Help hlp = new Help(); 

     if(Global.stop != -1){ 
      System.out.println("Help"); 
      hlp.run(); 
     } 

     if(Global.stop == -1) { 
      System.out.println("Main"); 
      mprog.run(); 
     } 
    } 
} 


class Help { 

    public static void run() { 
     String newline = System.getProperty("line.separator"); 

     System.out.print(newline); 
     System.out.println("Entering Help Mode!" + newline); 

     System.out.println("Entered Help Class"); 
     //String help = console.nextLine(); 
    } 
} 

class Run { 

    public static void run() { 

     /*EnterAll eall = new EnterAll(); 
     EnterCoords ecoords = new EnterCoords(); 
     EnterRelation erelat = new EnterRelation(); 
     EnterColor ecolor = new EnterColor(); 
     EnterShape eshape = new EnterShape(); 
     Coordinates coords = new Coordinates(); 
     Relation relat = new Relation(); 
     Color color = new Color(); 
     Shape shape = new Shape(); 
     List list = new List(); 
     Save save = new Save(); 
     SaveAs saveas = new SaveAs();*/ 

     String newline = System.getProperty("line.separator"); 
     Scanner console = new Scanner(System.in); 

     System.out.print(newline); 
     System.out.println("Initializing Main Program." + newline); 
     System.out.println("************************** MAIN MENU *************************" + newline); 
     System.out.println("Enter Coords \t Enter Relat \t Enter Color \t Enter Shape"+newline); 
     System.out.println("Coordinates \t Relation \t Color \t \t Shape" + newline); 
     System.out.println("Help \t \t List \t \t Save \t \t Save As" + newline); 
     System.out.println("**************************************************************" + newline); 

     System.out.print("Enter your selection or type All to enter lines consecutively: "); 
     String raw = console.nextLine(); 

     System.out.println(raw); 

     String select = errorCheck(raw); 

     if (select.equals("All")){ 
     } 

     if (select.equals("Enter Coords")){ 
     } 

     if (select.equals("Enter Relat")){ 
     } 

     if (select.equals("Enter Color")){ 
     } 

     if (select.equals("Enter Shape")){ 
     } 

     if (select.equals("Coordinates")){ 
     } 

     if (select.equals("Relation")){ 
     } 

     if (select.equals("Color")){ 
     } 

     if (select.equals("Shape")){ 
     } 

     if (select.equals("Help")){ 
     } 

     if (select.equals("List")){ 
     } 

     if (select.equals("Save")){ 
     } 

     if (select.equals("Save As")){ 
     } 
    } 

    private static String errorCheck(String raw) { 

     String select = raw; 
     return select; 
    } 
} 

回答

0

@Sean,这里是您的溶液

注释如下线

public class DataSort 
{ 
. 
. 
. 
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
//Scanner console = new Scanner(System.in); 
. 
. 
. 
//Global.stop = in.read(); 
. 
. 
} 

对于Global.stop = in.read(),从相同的输入读读取缓冲区(可能位于相同的类或其他位置)并根据需要解析字符串。不要创建另一个输入读取缓冲区。

Regards, Ravi

1

你的问题就出在

public class DataSort {...... Global.stop = in.read(); ......} 

因为in.read是用于读取整数输入。它不读取行尾字符。这就是为什么在输入选择字符串并回车后它变得无能为力。

问候, 拉维

+0

+1。为了澄清,'read()'读取单个字符,但将其作为整数返回。请参阅文档[此处](http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html)。 – Simon

+0

哦,这并不是我遇到麻烦,因为这只是检测任何键盘输入,然后告诉程序要去哪个类。问题出在Run类的String raw = console.nextLine(); –

+0

@Sean,在进入raw = console.nextLine()之前,它执行Global.stop = in.read()。这就是你得到控制台衣架的原因。 –