Tag: java.util.scanner

Java Scanner字符串输入

我正在编写一个使用Event类的程序,其中包含一个日历实例和一个String类型的描述。 创建事件的方法使用扫描程序来获取月,日,年,小时,分钟和描述。 我遇到的问题是Scanner.next()方法只返回空格前的第一个单词。 因此,如果输入是“我的生日”,则该事件实例的描述仅为“我的”。 我做了一些研究,发现人们使用Scanner.nextLine()来解决这个问题,但是当我尝试这个时,它只是跳过输入应该去的地方。 以下是我的代码的一部分: System.out.print(“Please enter the event description: “); String input = scan.nextLine(); e.setDescription(input); System.out.println(“Event description” + e.description); e.time.set(year, month-1, day, hour, min); addEvent(e); System.out.println(“Event: “+ e.time.getTime()); 这是我得到的输出: Please enter the event description: Event description Event: Thu Mar 22 11:11:48 EDT 2012 它跳过空格以输入描述字符串,因此,描述(最初设置为空格 – “”)永远不会更改。 我怎样才能解决这个问题?

如何确保Scanner hasNextInt()要求新输入?

新程序员在这里。 这可能是一个非常基本的问题,但它仍然让我感到困惑。 我正在尝试做的是编写一个只提供一个整数输入的方法,这样我就可以在主程序中使用该输入,而不必使用非整数输入。 然而,即使用自己的方法编写方法也是有问题的。 public static int goodInput () { Scanner input = new Scanner (System.in); //construct scanner boolean test = input.hasNextInt(); //set a sentinel value while (test == false) { //enter a loop until I actually get an integer System.out.println(“Integers only please”); //tell user to give me an integer test = input.hasNextInt(); //get new […]

Java Scanner不等待用户输入

我正在使用Java的扫描仪来读取用户输入。 如果我只使用一次nextLine,它可以正常工作。 有两个nextLine,第一个不等待用户输入字符串(第二个)。 输出: X:Y :(等待输入) 我的代码 System.out.print(“X: “); x = scanner.nextLine(); System.out.print(“Y: “); y = scanner.nextLine(); 任何想法为什么会这样? 谢谢

如何使用java在Array中获取用户输入?

如何使用Java在Array中获取用户输入? 即我们不是在我们的程序中自己初始化它,但用户将给它的价值..请指导!!

Java Scanner类读取字符串

我得到以下代码: int nnames; String names[]; System.out.print(“How many names are you going to save: “); Scanner in = new Scanner(System.in); nnames = in.nextInt(); names = new String[nnames]; for (int i = 0; i < names.length; i++){ System.out.print("Type a name: "); names[i] = in.nextLine(); } 该代码的输出如下: How many names are you going to save:3 Type a name: […]

Java的Scanner vs String.split()vs StringTokenizer; 我应该用哪个?

我目前正在使用split()来扫描一个文件,其中每一行都有’~’分隔的字符串数。 我在某处读到Scanner可以用一个长文件做得更好,性能方面,所以我想考虑一下。 我的问题是:我是否必须创建两个Scanner实例? 也就是说,一个读取一行而另一个基于该行来获取分隔符的标记? 如果我必须这样做,我怀疑我是否会从使用它中获得任何好处。 也许我在这里错过了一些东西?

当调用nextLine() – 方法时,为什么我不能在Scanner(System.in)中输入字符串?

这个程序实际上如何工作……? import java.util.Scanner; class string { public static void main(String a[]){ int a; String s; Scanner scan = new Scanner(System.in); System.out.println(“enter a no”); a = scan.nextInt(); System.out.println(“no is =”+a); System.out.println(“enter a string”); s = scan.nextLine(); System.out.println(“string is=”+s); } } 输出是: enter the no 1234 no is 1234 enter a string string is= //why is it […]

使用扫描仪读取行

为更多读者编辑 :问题是我的输入文件已损坏。 我不明白我做错了什么: 我使用的是这段代码: File f = new File(“C:\\Temp\\dico.txt”); BufferedReader r = null; try { r = new BufferedReader(new FileReader(f)); String scan; while((scan=r.readLine())!=null) { if(scan.length()==0) {continue;} //treatment } } catch (FileNotFoundException ex) { Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex); } finally { if(r!=null) try { r.close(); } catch (IOException ex) […]

Integer.parseInt(scanner.nextLine())vs scanner.nextInt()

我的教授倾向于做以下事情来从用户那里得到一个数字: Scanner scanner = new Scanner(System.in); Integer.parseInt(scanner.nextLine()); 与简单地执行scanner.nextInt()相比有什么好处? java.util.Scanner.java包含以下内容: public int nextInt() { return nextInt(defaultRadix); } public int nextInt(int radix) { // Check cached result if ((typeCache != null) && (typeCache instanceof Integer) && this.radix == radix) { int val = ((Integer)typeCache).intValue(); useTypeCache(); return val; } setRadix(radix); clearCaches(); // Search for next int try { […]

Java使用扫描仪输入密钥按下

我正在使用Java编程。 我正在尝试编写代码,该代码可以识别用户是否在基于控制台的程序中按下回车键。 我怎么能用java做到这一点。 有人告诉我,这可以使用Scanner或缓冲输入阅读器完成。 我不理解(或知道如何使用)缓冲输入阅读器。 我尝试使用扫描仪执行此操作,但在按两次输入后程序终止,但它不起作用 Scanner readinput = new Scanner(System.in); String enterkey = “Hola”; System.out.print(enterkey); enterkey = readinput.nextLine(); System.out.print(enterkey); if(enterkey == “”){ System.out.println(“It works!”); 谢谢 – 编辑 – 以下代码使用字符串的equals方法而不是== Scanner readinput = new Scanner(System.in); String enterkey = “Hola”; System.out.print(enterkey); enterkey = readinput.nextLine(); System.out.print(enterkey); if(enterkey.equals(“”)){ System.out.println(“It works!”); 如何做到这一点,以及使用缓冲输入阅读器做到这一点的优点是什么?