java.util.scanner

Exception in thread “main” java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862)

那年仲夏 提交于 2020-05-30 19:15:14
问题 I really don't see what the problem could be. This is the error I'm getting: $javac Palindrome.java $java -Xmx128M -Xms16M Palindrome Enter your word Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Palindrome.main(Palindrome.java:28) This is the code: import java.io.*; import java.util

Scanning doubles when I separate decimals with comma or dot

假如想象 提交于 2020-02-25 00:26:48
问题 I'm rather new to Java and I was making a simple calculator. Problem is when I my input number is for example "3.1" it gives an exception error, but when writing "3,1" it works just fine. My friend, however, has a slightly more advanced calculator (with string parsing) and when I run his code the opposite happens: 3,1 gives exception error, 3.1 works perfectly. I was looking forward to know what causes these different behaviors. I made this simple sum just now and the same happens, I'll edit

Scanning doubles when I separate decimals with comma or dot

删除回忆录丶 提交于 2020-02-25 00:13:02
问题 I'm rather new to Java and I was making a simple calculator. Problem is when I my input number is for example "3.1" it gives an exception error, but when writing "3,1" it works just fine. My friend, however, has a slightly more advanced calculator (with string parsing) and when I run his code the opposite happens: 3,1 gives exception error, 3.1 works perfectly. I was looking forward to know what causes these different behaviors. I made this simple sum just now and the same happens, I'll edit

Scanner without delimiter

青春壹個敷衍的年華 提交于 2020-02-24 04:35:06
问题 I would like to be able to parse strings like the following: "123456abcd9876az45678". The BNF is like this: number: ? definition of an int ? word: letter { , letter } expression: number { , word , number } However the class java.util.scanner doesn't allow me to do the following: Scanner s = new Scanner("-123456abcd9876az45678"); System.out.println(s.nextInt()); while (s.hasNext("[a-z]+")) { System.out.println(s.next("[a-z]+")); System.out.println(s.nextInt()); } Ideally, this should yield:

How to check if user input is String, double or long in Java

ぃ、小莉子 提交于 2020-02-14 04:53:08
问题 I'm a beginner in java. I want to check first if the user input is String or Double or int. If it's String, double or a minus number, the user should be prompted to enter a valid int number again. Only when the user entered a valid number should then the program jump to try. I've been thinking for hours and I come up with nothing useful.Please help, thank you! import java.util.InputMismatchException; import java.util.Scanner; public class Fizz { public static void main(String[] args) { System

Scanner error that I can't figure out: NoSuchElementException

心已入冬 提交于 2020-02-02 13:43:08
问题 It's crashing on the third line inside the do-while loop, and doesn't wait for my input: input = kb.nextInt(); Stack trace: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at main.MainDriver.main(MainDriver.java:50) Relevant code: do { displayFullMenu(); System.out.print("Selection: "); input = kb.nextInt(

Scanner error that I can't figure out: NoSuchElementException

亡梦爱人 提交于 2020-02-02 13:42:05
问题 It's crashing on the third line inside the do-while loop, and doesn't wait for my input: input = kb.nextInt(); Stack trace: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at main.MainDriver.main(MainDriver.java:50) Relevant code: do { displayFullMenu(); System.out.print("Selection: "); input = kb.nextInt(

Scanner is skipping nextLine() after using next() or nextFoo()?

我的未来我决定 提交于 2020-01-30 12:28:07
问题 I am using the Scanner methods nextInt() and nextLine() for reading input. It looks like this: System.out.println("Enter numerical value"); int option; option = input.nextInt(); // Read numerical value from input System.out.println("Enter 1st string"); String string1 = input.nextLine(); // Read 1st string (this is skipped) System.out.println("Enter 2nd string"); String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value) The problem is that after

Splitting a string into different array indexes Java?

不打扰是莪最后的温柔 提交于 2020-01-24 19:52:12
问题 I am trying to split a string into different array indexes. This string is coming from user input (through java.util.Scanner ) and is being loaded into a String variable. How can I split the input from the string into different array indexes? Also, how can I do the math functions that are implied by DOB being an int ? Here is my code: import java.util.Scanner; public class main { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println("Enter date of

Java Scanner - Delimit by spaces unless quotation marks are present?

你离开我真会死。 提交于 2020-01-24 11:04:54
问题 I'm trying to use the Scanner class in Java to get data from a configuration file. The file's elements are delimited by whitespace. However, if a phrase or element should be interpreted as a string literal (including whitespace), then double or single-quotes are places around the element. This gives files that look like this: > R 120 Something AWord > P 160 SomethingElse "A string literal" When using the Java Scanner class, it delimits by just whitespace by default. The Scanner class has the