java.util.scanner

Java while loop terminates after one interation with scan.nextLine(); method

岁酱吖の 提交于 2019-12-13 01:05:53
问题 I am a beginning Computer Science student and currently stuck with one problem. It's a simple program that asks the user for a number x , then solves a Polynomial equation for that number. Afterwards, it is supposed to ask the user if he wants to continue, and if so, a new number for x is prompted. However, this program only asks the user for x once, and then terminates after evaluating the Polynomial. It even prints Continue? but doesn't even wait to read in the next line, it seems to

Trying to read 2 words from a file in Java

感情迁移 提交于 2019-12-12 21:24:24
问题 I'm trying to write a simple program to read a text file and store pair of words in a Set . Here is the code I wrote for that import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String[] args) { TreeSet<String> phraseSet = new TreeSet<String>(); try { Scanner readfile = new Scanner(new File("data.txt")); while(readfile.hasNext("\\w{2}")) { String phrase = readfile.next("\\w{2}"); phraseSet

java.util.Scanner : why my nextDouble() does not prompt?

此生再无相见时 提交于 2019-12-12 21:19:05
问题 import java.util.*; public class June16{ public static void main(String[] args){ Scanner kb = new Scanner(System.in); double b=0; boolean checkInput = true; do{ try{ System.out.println("Input b : "); b = kb.nextDouble(); checkInput = false; }catch(InputMismatchException ime){ } }while(checkInput); } } After InputMismatchException is thrown, why my program not prompt for input? :D 回答1: From the documentation: When a scanner throws an InputMismatchException, the scanner will not pass the token

How to read double in java from a file

旧时模样 提交于 2019-12-12 20:37:16
问题 I'm trying to read a double from a file but I have this exception: java.util.InputMismatchException. I've tried to do the useLocale(Locale.US) but it doesn't work. This is my code public static void main(String[] args){ System.out.println("Introduce the name of the file"); Scanner teclat = new Scanner(System.in); teclat.useLocale(Locale.US); Scanner fitxer = new Scanner(new File(teclat.nextLine())); while(fitxer.hasNext()){ String origen=fitxer.next(); String desti=fitxer.next(); double

Meaning of scanner isn't synchronized

半城伤御伤魂 提交于 2019-12-12 20:06:21
问题 I was going through the differences between scanner and BufferedReader in Java and one point which I could not understand was that said Scanner is not synchronized while BufferedReader is. Now can anyone please explain what it means? 回答1: Literally, it means what it says. Key operations of the BufferedReader API are implemented using synchronized blocks, and the equivalent operations in Scanner are not. This means that a BufferedReader can be "safely" shared between multiple threads 1 ,

Code running stuck at scanner.hasNextLine()

烈酒焚心 提交于 2019-12-12 19:07:32
问题 I try to make a little Server-Client connection. They both have a Scanner and a PrintWriter, and they are writing to each other using a Socket's input and output stream. Client.java: import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client { static ServerSocket serverSocket; static Socket socket; static PrintWriter printWriter; static Scanner scanner; public

Type safety of input.nextLine?

瘦欲@ 提交于 2019-12-12 16:10:07
问题 Would there ever be a case where an exception could be thrown by assigning the value of input.nextLine() to a String variable with the Scanner ? Like if you put String foo = input.nextInt(); You would get an InputMismatchException . So what I'm wondering is if there's any possible way to get an exception from: String foo = input.nextLine(); It might be a dumb question but I need to be absolutely sure. Thanks in advance! 回答1: The answer in the docs: Throws: NoSuchElementException - if no line

Java - Scanner : reading line of record separated by Pipe | as delimiter

只愿长相守 提交于 2019-12-12 12:46:59
问题 I want to read a text file where fields data are separated by delimiter | (pipe symbol). But some unexpected happened : Here is my code : doScannerTest ("Y~2011~GT~Nepal~Ganesh~Tiwari~N", "~"); doScannerTest("Y|2011|GT|Nepal|Ganesh|Tiwari|N", "|"); private static void doScannerTest(String recordLine, String delim) { java.util.Scanner lineScanner = new java.util.Scanner(recordLine); lineScanner.useDelimiter(delim); while (lineScanner.hasNext()) { System.out.println(lineScanner.next()); } } The

Is there any way I can use a Scanner object in a different class?

风流意气都作罢 提交于 2019-12-12 10:22:33
问题 So I have a simple Scanner object: Scanner scan = new Scanner (System.in); I am trying to use this Scanner object inside a method that I have declared in a different class. Is there anyway this can be done? I have also tried using: public static final Scanner scan = new Scanner(System.in); but this didn't really help 回答1: public static final Scanner scan = new Scanner(System.in); As you have declared Scanner instance as public and static . So you access it using the Class reference. Code in

How do you read a text file and print it to the console window? Java

杀马特。学长 韩版系。学妹 提交于 2019-12-12 10:15:18
问题 I would like to read an entire text file and store its entire contents into a single string. Then I would like to print the string to the console window. I tried this: import java.util.Scanner; import java.io.*; public class WritingTextFiles{ public static void main (String [] args) throws IOException{ FileWriter fw= new FileWriter("testing.txt"); Scanner in= new Scanner (System.in); String testwords=in.nextLine(); fw.write(testwords); BufferedReader r = new BufferedReader( new FileReader(