java.util.scanner

Java reading multiple ints from a single line

久未见 提交于 2019-11-26 12:24:55
问题 I am working on a program and I want to allow a user to enter multiple integers when prompted. I have tried to use a scanner but I found that it only stores the first integer entered by the user. For example: Enter multiple integers: 1 3 5 The scanner will only get the first integer 1. Is it possible to get all 3 different integers from one line and be able to use them later? These integers are the positions of data in a linked list I need to manipulate based on the users input. I cannot post

Read next word in java

℡╲_俬逩灬. 提交于 2019-11-26 12:19:14
问题 I have a text file that has following content: ac und accipio annehmen ad zu adeo hinzugehen ... I read the text file and iterate through the lines: Scanner sc = new Scanner(new File(\"translate.txt\")); while(sc.hasNext()){ String line = sc.nextLine(); } Each line has two words. Is there any method in java to get the next word or do I have to split the line string to get the words? 回答1: You do not necessarily have to split the line because java.util.Scanner's default delimiter is whitespace.

Reading a .txt file using Scanner class in Java

大憨熊 提交于 2019-11-26 11:44:34
I am working on a Java program that reads a text file line-by-line, each with a number, takes each number throws it into an array, then tries and use insertion sort to sort the array. I need help with getting the program to read the text file. I am getting the following error messages: java.io.FileNotFoundException: 10_Random (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.util.Scanner.<init>(Unknown Source) at insertionSort.main(insertionSort.java:14) I have a copy of the .txt file in my "src"

Exception in thread “main” java.io.FileNotFoundException: Error

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 11:32:52
问题 I am using Eclipse to compile and run my java codes. Here is Error I am getting. Exception in thread \"main\" java.io.FileNotFoundException: file.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.util.Scanner.<init>(Unknown Source) at helloworld.main(helloworld.java:9) Here is my Code import java.io.File; import java.io.IOException; import java.util.Scanner; public class helloworld { public

Java scanner not going through entire file

假如想象 提交于 2019-11-26 11:26:24
问题 I\'m writing a program in Java and one of the things that I need to do is to create a set of every valid location for a shortest path problem. The locations are defined in a .txt file that follows a strict pattern (one entry per line, no extra whitespace) and is perfect for using .nextLine to get the data. My problem is that 241 lines into the file (out of 432) the scanner stops working 3/4 of the way through an entry and doesn\'t recognize any new lines. My code: //initialize state space

Can&#39;t use Scanner.nextInt() and Scanner.nextLine() together [duplicate]

故事扮演 提交于 2019-11-26 11:07:50
问题 This question already has answers here : Scanner is skipping nextLine() after using next() or nextFoo()? (16 answers) Closed 3 years ago . I have to get a string input and an integer input, but there order of input should be that integer comes first then user should be asked for string input Scanner in = new Scanner(System.in); input = in.nextLine(); k = in.nextInt(); in.close(); The above code works fine but if I take an integer input first like in the following code Scanner in = new Scanner

Scanner input validation in while loop

◇◆丶佛笑我妖孽 提交于 2019-11-26 10:02:09
问题 I\'ve got to show Scanner inputs in a while loop: the user has to insert inputs until he writes \"quit\". So, I\'ve got to validate each input to check if he writes \"quit\". How can I do that? while (!scanner.nextLine().equals(\"quit\")) { System.out.println(\"Insert question code:\"); String question = scanner.nextLine(); System.out.println(\"Insert answer code:\"); String answer = scanner.nextLine(); service.storeResults(question, answer); // This stores given inputs on db } This doesn\'t

Java using scanner enter key pressed

南笙酒味 提交于 2019-11-26 10:01:32
问题 I am programming using Java. I am trying write code which can recognize if the user presses the enter key in a console based program. How can I do this using java. I have been told that this can be done using either Scanner or, buffered input reader. I do not understand(or know how to use) buffered input reader. I tried to do do this using scanner but after pressing enter twice the program terminates, and it doesn\'t work Scanner readinput = new Scanner(System.in); String enterkey = \"Hola\";

Exception in thread “main” java.util.NoSuchElementException

耗尽温柔 提交于 2019-11-26 09:59:06
问题 Whenever I run this, the chooseCave() function works fine with the in.nextInt() . When I choose the cave, the messages pop up at 2-second intervals, and then as soon as it gets past that part, it gives me the error: Exception in thread \"main\" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Dragon.main(Dragon.java:81) I have tried hasNextLine() and hasNextInt() , and when I use while hasNextLine() in the main method, I get a ton more errors.

Java: Infinite loop using Scanner in.hasNextInt()

妖精的绣舞 提交于 2019-11-26 09:44:03
问题 I am using the following code: while (invalidInput) { // ask the user to specify a number to update the times by System.out.print(\"Specify an integer between 0 and 5: \"); if (in.hasNextInt()) { // get the update value updateValue = in.nextInt(); // check to see if it was within range if (updateValue >= 0 && updateValue <= 5) { invalidInput = false; } else { System.out.println(\"You have not entered a number between 0 and 5. Try again.\"); } } else { System.out.println(\"You have entered an