java.util.scanner

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

杀马特。学长 韩版系。学妹 提交于 2020-01-05 05:58: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

Issue with java Scanner not taking nextLine on new instance

本秂侑毒 提交于 2020-01-05 02:37:08
问题 package sandbox2; import java.util.Scanner; public class Sandbox2 { public static void main(String[] args) { for (int i = 0; i < 5; i++) { String s = askForProperty("Enter value for " + i + ": "); System.out.println(i + " is: " + s); } } private static String askForProperty(String message) { Scanner keyboard = new Scanner(System.in); System.out.print(message); String s = keyboard.nextLine(); keyboard.close(); return s; } } When i run the above code, it returns the first response PERFECTLY.

java.util.Scanner does not return to Prompt

家住魔仙堡 提交于 2020-01-05 01:25:12
问题 import java.util.Scanner; class newClass { public static void main(String args[]) { Scanner s = new Scanner(System.in); while (s.hasNext()) { System.out.println(s.next()); } s.close(); } } This program does not return to prompt (I have been running it through the Terminal). Why is that? How can I correct it? 回答1: This program does not return to prompt (I have been running it through the Terminal). Why is that? Because s.hasNext() will block until further input is available and will only

java.util.Scanner does not return to Prompt

那年仲夏 提交于 2020-01-05 01:25:08
问题 import java.util.Scanner; class newClass { public static void main(String args[]) { Scanner s = new Scanner(System.in); while (s.hasNext()) { System.out.println(s.next()); } s.close(); } } This program does not return to prompt (I have been running it through the Terminal). Why is that? How can I correct it? 回答1: This program does not return to prompt (I have been running it through the Terminal). Why is that? Because s.hasNext() will block until further input is available and will only

Copying a binary file outside of a jar

时光总嘲笑我的痴心妄想 提交于 2020-01-04 09:37:22
问题 I have an exe packaged inside my jar file and I am trying to copy it to a temporary location so that i can run it using Desktop.browse() , to do this I set up a scanner with the input stream constructor using class.getResourceAsStream , and then with a printwriter wrote that all to a file. The problem that occurred says that the exe is invalid. I think this is due to some binary data being lost. If anyone can help please post a comment. Scanner sc = new Scanner(ClassBuilder.class

Copying a binary file outside of a jar

亡梦爱人 提交于 2020-01-04 09:37:02
问题 I have an exe packaged inside my jar file and I am trying to copy it to a temporary location so that i can run it using Desktop.browse() , to do this I set up a scanner with the input stream constructor using class.getResourceAsStream , and then with a printwriter wrote that all to a file. The problem that occurred says that the exe is invalid. I think this is due to some binary data being lost. If anyone can help please post a comment. Scanner sc = new Scanner(ClassBuilder.class

Scanner, nextInt and InputMismatchException

吃可爱长大的小学妹 提交于 2020-01-04 05:27:44
问题 I'm trying to read a text file and then print out the integers in a loop using the nextInt() function in Java. The text file I have is of the form: a 2000 2 b 3000 1 c 4000 5 d 5000 6 Here is my code: public static void main(String[] args) throws FileNotFoundException { String fileSpecified = args[0] + ".txt"; FileReader fr = new FileReader(fileSpecified); BufferedReader br = new BufferedReader (fr); Scanner in = new Scanner (br); while (in.hasNextLine()) { System.out.println ("next int = " +

**Exception in thread “main” java.util.InputMismatchException**

蓝咒 提交于 2020-01-02 09:36:06
问题 I am trying to fetch some records from a txt file and put those in Database in the following Java Program package Java_Demo; import java.sql.*; import java.util.*; import java.io.*; public class Jdbc_Demo { public static void main(String ...args)throws ClassNotFoundException,SQLException,FileNotFoundException { FileInputStream fin=new FileInputStream("C:/Users/steve-pc/Desktop/Employees.txt"); Scanner s=new Scanner(fin); s.useDelimiter(",|\\n"); Class.forName("oracle.jdbc.driver.OracleDriver"

Problems with Scanner - Java

老子叫甜甜 提交于 2020-01-01 15:07:17
问题 I'm trying to have the option of reading a string with multiple words, ie. Los Angeles or New York City. Using scanner.next() for "Departure" and "Arrival" would only read the first one if there were two words and split them between variables. nextLine() has not been much luck either. Here's my code: System.out.print("\nEnter flight number: "); int flightNumber = Integer.valueOf(scanner.nextLine()); System.out.print("\nEnter departing city: "); String departingCity = scanner.nextLine();

Java Scanner no line found, and then Scanner closed error?

偶尔善良 提交于 2019-12-31 07:24:07
问题 I have Java code that asks for user input and then stores this data in a string variable. The below function is part of a class called 'number' and is called in the main function. public static void setVal(int i){ Scanner readIn = new Scanner(System.in); //while (readIn.hasNextLine()){ str = readIn.nextLine(); numCheck = false; if (i == 1){ while (!numCheck){ if (str.contains(" ")){ System.out.println("Please input a single item."); str = readIn.nextLine(); } else if (!isNumeric(str)){ System