java.util.scanner

Scanner doesn't read whole sentence - difference between next() and nextLine() of scanner class

烈酒焚心 提交于 2019-11-28 09:45:49
I'm writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say only 'Archbishop' from 'Archbishop Street'. How do I fix this? import java.util.*; class MyStudentDetails{ public static void main (String args[]){ Scanner s = new Scanner(System.in); System.out.println("Enter Your Name: "); String name = s.next(); System.out.println("Enter Your Age: "); int age = s.nextInt(); System.out.println("Enter Your E-mail: "); String email = s.next(); System.out.println("Enter Your Address: ");

Java Scanner why is nextLine() in my code being skipped? [duplicate]

谁说我不能喝 提交于 2019-11-28 09:40:31
问题 This question already has an answer here: Scanner is skipping nextLine() after using next() or nextFoo()? 16 answers Scanner kb = new Scanner(System.in); System.out.println("Inserting L"); int L = kb.nextInt(); System.out.println("Inserting N"); int N = kb.nextInt(); System.out.println("Inserting x"); String x = kb.nextLine(); System.out.println(x); System.out.println("You inputed L,N,x"); Why is there no prompt for nextLine() in this code? 回答1: Use: String x = kb.next(); instead of kb

Why is this code getting a Java NoSuchElement exception?

拈花ヽ惹草 提交于 2019-11-28 08:56:18
问题 I have traced through this code and can't figure out how to fix it. When running the code why wouldn't the user be prompted for input rather than Java determining that there is no input? Error trace below. import java.util.*; public class SortAsInserted { public static void main(String[] args) { int array_size = GetArraySize(); //System.out.println(array_size); String[] myArray = new String[array_size]; for (int i = 0; i < array_size; i++){ String next_string = GetNextString(); System.out

Why is hasNext() False, but hasNextLine() is True?

前提是你 提交于 2019-11-28 08:51:28
Question How is it that for a scanner object the hasNextLine() method returns true while the hasNext() method returns false? Note: Based on the input file, the hasNext() method is returning the result as expected; the hasNextLine() does not seem to be returning the correct result. Code Here's the code I'm running that's creating the results below: public void ScannerTest(Reader fileReaderObject){ Scanner scannerObj = new Scanner(fileReaderObject); for(int i = 1; scannerObj.hasNext(); i++){ System.out.println(i + ": " + scannerObj.next()); System.out.println("Has next line: " + scannerObj

Java Scanner question

杀马特。学长 韩版系。学妹 提交于 2019-11-28 08:36:18
How do you set the delimiter for a scanner to either ; or new line? I tried: Scanner.useDelimiter(Pattern.compile("(\n)|;")); But it doesn't work. Powerlord As a general rule, in patterns, you need to double the \ . So, try Scanner.useDelimiter(Pattern.compile("(\\n)|;"));` or Scanner.useDelimiter(Pattern.compile("[\\n;]"));` Edit : If \r\n is the problem, you might want to try this: Scanner.useDelimiter(Pattern.compile("[\\r\\n;]+")); which matches one or more of \r , \n , and ; . Note : I haven't tried these. As you've discovered, you needed to look for DOS/network style \r\n (CRLF) line

Scanner Exception Retry

狂风中的少年 提交于 2019-11-28 08:08:23
问题 How to make scanner retry when exception occur? Consider this app running on CLI mode. Example: System.out.print("Define width: "); try { width = scanner.nextDouble(); } catch (Exception e) { System.err.println("That's not a number!"); //width = scanner.nextDouble(); // Wrong code, this bring error. } If the user not inputting double type input, then the error thrown. But i want after the error message appears. It's should be asking the user input the width again. How to do that? 回答1: If I

How to test for blank line with Java Scanner?

空扰寡人 提交于 2019-11-28 06:31:22
I am expecting input with the scanner until there is nothing (i.e. when user enters a blank line). How do I achieve this? I tried: while (scanner.hasNext()) { // process input } But that will get me stuck in the loop Here's a way: Scanner keyboard = new Scanner(System.in); String line = null; while(!(line = keyboard.nextLine()).isEmpty()) { String[] values = line.split("\\s+"); System.out.print("entered: " + Arrays.toString(values) + "\n"); } System.out.print("Bye!"); From http://www.java-made-easy.com/java-scanner-help.html : Q: What happens if I scan a blank line with Java's Scanner? A: It

How to make Scanner Properly Read Escape Characters?

不打扰是莪最后的温柔 提交于 2019-11-28 04:04:33
问题 I'm reading from a file that reads something like all on one line: Hello World!\nI've been trying to get this to work for a while now.\nFrustrating.\n And my Scanner reads that from the file and puts it in a String: Scanner input = new Scanner(new File(fileName)); String str = input.nextLine(); System.out.print(str); Now, I want the output then to be: Hello World! I've been trying to get this work for a while now. Frustrating. But instead I'm getting the exact same thing as the input. That is

Scanner close after use

烂漫一生 提交于 2019-11-28 02:21:30
While using scanner like following: Scanner s = new Scanner(System.in); String response = s.next(); Boolean approved = (response.contains("Y") || response.contains("y")) ? true : false; if (approved){ Do Stuff } s.close(); I'm getting no such Element exception exception: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source)**** I'm calling s (Scanner) multiple times, the runtime error occur on the second call. This is due to closing the scanner and than probably using it again. My question is, I'm

Does the Scanner class load the entire file into memory at once?

六眼飞鱼酱① 提交于 2019-11-28 02:03:08
I often use the Scanner class to read files because it is so convenient. String inputFileName; Scanner fileScanner; inputFileName = "input.txt"; fileScanner = new Scanner (new File(inputFileName)); My question is, does the above statement load the entire file into memory at once? Or do subsequent calls on the fileScanner like fileScanner.nextLine(); read from the file (i.e. from external storage and not from memory)? I ask because I am concerned about what might happen if the file is too huge to be read into memory all at once. Thanks. Edwin Dalorzo If you read the source code you can answer