java.util.scanner

Continuous input commands

坚强是说给别人听的谎言 提交于 2019-12-08 07:31:01
问题 My program will read user keyboard commands in the form of " command parameter " with a space in between. It keeps carrying out individual commands until the next command is " exit ". Also, if the user messes up, the program should show an error but continue asking for commands (a feature I don't think I have completed).. Is the following code a good way of implementing this? Could it handle the user simply pressing the enter key w/o a command, junk input, etc? If anything, I would love to

How can i detect BlankLine that Scanner has receive?

穿精又带淫゛_ 提交于 2019-12-08 05:26:33
问题 I want to get data form text files ,and I use Scanner to get data form text file. It's profile save pattern name status friend friend . . (Blank line) Blank Line is separate each profile.(friend will looping till next line is a Blank Line) john happy james james sad john And i code to get file form text like this try{ Scanner fileIn = new Scanner(new FileReader("testread.txt")); while(fileIn.hasNextLine()){ String line = fileIn.nextLine(); String linename = fileIn.nextLine(); String

Scanner throwing NoSuchElementException

≡放荡痞女 提交于 2019-12-08 03:38:03
问题 I am trying to create a simple small program that will ask for a positive int and won't crash or close until it has received a positive int from the user. However my program keeps crashing, and reporting the error NoSuchElementException, when it calls the method with Scanner in it more than once. I will be using the fundamentals of this program to help some other things I am working on. Here is my current code; import java.util.InputMismatchException; import java.util.Scanner; public class

How to stop Java Scanner from accepting input

ε祈祈猫儿з 提交于 2019-12-08 02:56:37
问题 So, I am working on a quiz taking program that works from the command line and the quizzes have time limits. What I want to do, is to stop the quiz right when the user's time is up even if they're in the middle of answering a question. I am using Java's Scanner to get the user's input, so I want to essentially tell the Scanner object to terminate even if it's in the middle of accepting input. Now, I know that I can retroactively punish a user for going over time after the fact, but I simply

Java: using hasNextInt with do-while loop, it ignores integer inputs at even times

安稳与你 提交于 2019-12-08 01:32:24
问题 I am trying to learn Java programming by myself and came across the course CS106A provided by Stanford. It's a great free online course. I watched several of the lecture videos and I enjoyed them so far. I am now trying to do the assignments and I have this problem I can't solve by myself. It is the number 5 of this assignment. Basically, it requires the learner to create a console program to get some integers input by the user and in response, showing the biggest and smallest number. The

Java Iterate array with a fixed length and take values with Scanner class

偶尔善良 提交于 2019-12-07 23:58:13
问题 How can i get the value of a java array using Scanner class with a fixed length of 2, and which will iterate until its value is equal to a given value? For example; for the following inputs, A G N H D F I wrote a for loop to take the values of fixed array road, in which the length is 2 using Scanner class. for(int i = 0; i<block.length; i++){ System.out.println("enter number"); block[i]=input2.next().charAt(0); } I want to iterate this loop while the user input is {'C','C'}. THat means the

Scanner error that I can't figure out: NoSuchElementException

心不动则不痛 提交于 2019-12-07 17:42:29
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(); switch (input) { //Create new survey case 1: currentSurvey = new Survey(); break; //Display current

How do I close a scanner in java?

五迷三道 提交于 2019-12-07 15:41:14
问题 When ever I run the following code, private static String input(String Message){ Scanner input_scanner = new Scanner(System.in); System.out.print("\n" + Message); String string = input_scanner.nextLine(); input_scanner.close(); return string; } I get this error: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at main.learn.input(learn.java:25) at main.learn.main(learn.java:13) I figured out it was something to do with

Scanner nextLine() occasionally skips input

不羁的心 提交于 2019-12-07 10:10:25
问题 Here is my code Scanner keyboard = new Scanner(System.in); System.out.print("Last name: "); lastName = keyboard.nextLine(); System.out.print("First name: "); firstName = keyboard.nextLine(); System.out.print("Email address: "); emailAddress = keyboard.nextLine(); System.out.print("Username: "); username = keyboard.nextLine(); and it outputs this Last name: First name: Basically it skips letting me enter lastName and goes straight to the prompt for firstName . However, if I use keyboard.next()

Scanner.hasNext() returns false

删除回忆录丶 提交于 2019-12-07 08:58:31
问题 I have directory with many files in it - each with over 800 lines in it. Hovewer, when I try to read it using Scanner, it seems empty. File f1 = new File("data/cityDistances/a.txt"), f2 = new File("data/cityDistances/b.txt"); System.out.println(f1.exists() && f2.exists()); //return true System.out.println(f1.getTotalSpace() > 0 && f2.getTotalSpace() > 0); //return true Scanner in = new Scanner(f1); System.out.println(in.hasNext()); // return false; System.out.println(in.hasNextLine()); /