Tag: java.util.scanner

在String中提取整数部分

提取字符串的整数部分的最佳方法是什么 Hello123 你如何获得123部分。 您可以使用Java的Scanner来破解它,有更好的方法吗?

nextInt()的扫描程序错误

我试图使用扫描仪从键盘获取int,但我收到以下错误: Exception in thread “main” java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at TableReader.mainMenu(TableReader.java:122) at TableReader.main(TableReader.java:76) 这就是我所拥有的。 它独立于我的其余程序,我不明白为什么这不起作用。 如果有帮助的话,它会在while循环中调用的方法中声明。 // scan for selection Scanner s = new Scanner(System.in); int choice = s.nextInt(); // error occurs at this line s.close(); 我介绍了调试器并将错误缩小到: Java运行时环境检测到致命错误:在pc = 0xb6bdc8a8处为SIGSEGV(0xb),pid = 5587,tid = 1828186944 JRE版本:7.0_07-b30 Java VM:OpenJDK服务器VM(23.2-b09混合模式linux-x86)有问题的框架:V [libjvm.so + 0x4258a8] […]

扫描仪只读取文件名而不读取任何其他内容

我正在努力实现一个基本的词法分析器。 我现在仍然坚持解析文件。 public ArrayList ParseFile () { int lineIndex = 0; Scanner scanner = new Scanner(this.fileName); while (scanner.hasNextLine()) { lineIndex++; String line = scanner.nextLine(); if (line.equals(“”)) continue; String[] split = line.split(“\\s”); for (String s : split) { if (s.equals(“”) || s.equals(“\\s*”) || s.equals(“\t”)) continue; Token token = new Token(s, lineIndex); parsedFile.add(token); } } scanner.close(); return […]

Java多个扫描仪

我有一个类创建多个Integer对象并将它们放入LinkedList ,如下所示: public class Shares implements Queue { protected LinkedList L; public Shares() { L = new LinkedList(); } public boolean add(E price) { System.out.println(“How many of these shares would you like?”); Scanner scanInt; scanInt = new Scanner(System.in); Integer noShares = scanInt.nextInt(); for (int i = 0; i < noShares; i++) { L.addLast(price); } scanInt.close(); […]

为什么我得到InputMismatchException?

到目前为止我有这个: public double checkValueWithin(int min, int max) { double num; Scanner reader = new Scanner(System.in); num = reader.nextDouble(); while (num max) { System.out.print(“Invalid. Re-enter number: “); num = reader.nextDouble(); } return num; } 和这个: public void askForMarks() { double marks[] = new double[student]; int index = 0; Scanner reader = new Scanner(System.in); while (index < […]

为什么hasNextLine()永远不会结束?

对不起,如果听起来太简单了。 我是Java的新手。 这是我用来检查hasNextLine()一些简单代码。 当我运行它时,我无法阻止它。 我想如果你没有写任何输入并按Enter键 ,你就会逃脱while循环。 有人可以向我解释hasNextLine()在这种情况下是如何工作的吗? import java.util.*; public class StringRaw { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String str = sc.nextLine(); } System.out.print(“YOU’VE GOT THROUGH”); } }

如何使用Scanner仅接受有效的int作为输入

我正在尝试使一个小程序更强大,我需要一些帮助。 Scanner kb = new Scanner(System.in); int num1; int num2 = 0; System.out.print(“Enter number 1: “); num1 = kb.nextInt(); while(num2 < num1) { System.out.print("Enter number 2: "); num2 = kb.nextInt(); } 数字2必须大于数字1 此外,我希望程序自动检查并忽略用户是否输入字符而不是数字。 因为现在当用户输入例如r而不是数字时,程序才会退出。

扫描方法获取char

什么是Scanner方法来获取Java中键盘返回的char 。 像用于String nextLine() ,用于int nextInt()等。

扫描仪NoSuchElementException

我的Java分配有问题。 我遇到了一个意想不到的exception,特别是: java.util.NoSuchElementException:找不到行 我正在使用Scanner(System.in) ,程序不断读取任何内容并重复“无效格式”exception文本。 如果我输入一个正确值int ,第一部分通过,然后double部分立即进入此exception。 如果我输入一个错误值的int ,那么它开始循环exception。 这是我的代码: import java.util.Scanner; public class Program_4 { public static void main(String[] args) { getValidInt(“Enter an integer from 5 to 50”,5,50); getValidDouble(“Enter a double from 5.0 to 50.0”,5.0,50.0); getValidString(“Enter a string with length from 5 to 8 characters”,5,8); } public static int getInt(String prompt) { Scanner sc […]

如何使用Scanner处理由无效输入(InputMismatchException)引起的无限循环

所以,我对这段代码感到困惑: import java.util.InputMismatchException; import java.util.Scanner; public class ConsoleReader { Scanner reader; public ConsoleReader() { reader = new Scanner(System.in); //reader.useDelimiter(System.getProperty(“line.separator”)); } public int readInt(String msg) { int num = 0; boolean loop = true; while (loop) { try { System.out.println(msg); num = reader.nextInt(); loop = false; } catch (InputMismatchException e) { System.out.println(“Invalid value!”); } } return […]