java.util.scanner

what are the benefits of BufferedReader over Scanner

折月煮酒 提交于 2019-12-01 11:25:32
here's a code about depth first search in graphs. who knows why bufferedReader class were used in this code? and why nextInt function not used instead? what is its privilege? is it for speeding up processing? Thanks :) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Graph { int g[][]; int v,e; int visited[]; void createGraph()throws IOException { int a,b; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\n Enter Number Of Vertices = "); v=Integer.parseInt(br.readLine()); System.out.print("\n Enter

Stop scanner from reading user input java?

时光毁灭记忆、已成空白 提交于 2019-12-01 11:24:23
I am trying to write this method, which keeps reading from the user, until the word "exit" is inputted. I tried with a break and for loop; it didn't work. I was trying with while, but it does not stop even when the word "exit" is inputted. Any ideas how to fix this? Thanks. public void read(Scanner scanner){ while(3<4){ String in = scanner.nextLine(); Scanner scanner_2 = new Scanner(in); String name = null; if(scanner_2.hasNext()){ //create and add the user to the user container class name = scanner_2.next(); System.out.println(name); } if(name == exit) //stop or break the while loop } } name

Infinite loop when using scanner? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-01 10:56:50
问题 This question already has answers here : How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner (5 answers) Closed 3 years ago . boolean z = false; do { try { a = sc.nextInt(); z = true; } catch(Exception e) { } } while(!z); Try this. If you try an integer the first time it executes properly. However if you enter the wrong type of text it turns into an infinite loop even if you enter an int next and skips assigning the boolean value to true. Why is this?

How to ignore first line of .txt when using Scanner class

守給你的承諾、 提交于 2019-12-01 10:51:44
I have a text file that reads: Description|SKU|Retail Price|Discount Tassimo T46 Home Brewing System|43-0439-6|17999|0.30 Moto Precise Fit Rear Wiper Blade|0210919|799|0.0 I've got it so that I read everything, and it works perfectly, save for the fact that it reads the first line, which is a sort of legend for the .txt file, which must be ignored. public static List<Item> read(File file) throws ApplicationException { Scanner scanner = null; try { scanner = new Scanner(file); } catch (FileNotFoundException e) { throw new ApplicationException(e); } List<Item> items = new ArrayList<Item>(); try

How to check Scanner.hasNext(System.in) with a timeout if nothing is typed?

风格不统一 提交于 2019-12-01 10:26:58
问题 Scanner sc = new Scanner(System.in); Scanner can be used for reading text files, user input streams, and more. I am specifically using it for reading user input as is made clear above. With the Scanner that I made above, because it 'rides' System.in, a call to its hasNext() when there is no next input will cause the relevant thread to block until it has data next and return true when it does. I would like to check, getter style, weather there is data next, not, like hasNext() returns, weather

How to ignore first line of .txt when using Scanner class

半世苍凉 提交于 2019-12-01 09:38:33
问题 I have a text file that reads: Description|SKU|Retail Price|Discount Tassimo T46 Home Brewing System|43-0439-6|17999|0.30 Moto Precise Fit Rear Wiper Blade|0210919|799|0.0 I've got it so that I read everything, and it works perfectly, save for the fact that it reads the first line, which is a sort of legend for the .txt file, which must be ignored. public static List<Item> read(File file) throws ApplicationException { Scanner scanner = null; try { scanner = new Scanner(file); } catch

what are the benefits of BufferedReader over Scanner

ε祈祈猫儿з 提交于 2019-12-01 08:45:10
问题 here's a code about depth first search in graphs. who knows why bufferedReader class were used in this code? and why nextInt function not used instead? what is its privilege? is it for speeding up processing? Thanks :) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Graph { int g[][]; int v,e; int visited[]; void createGraph()throws IOException { int a,b; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out

Fast & Efficient Way To Read Large JSON Files Line By Line in Java

给你一囗甜甜゛ 提交于 2019-12-01 08:29:05
I have 100 millions of records in JSON file, need an efficient and fastest method to read the array of arrays from a JSON file in java . JSON file look like: [["XYZ",...,"ABC"],["XYZ",...,"ABC"],["XYZ",...,"ABC"],...,["XYZ",...,"ABC"], ["XYZ",...,"ABC"],["XYZ",...,"ABC"],["XYZ",...,"ABC"],...,["XYZ",...,"ABC"], ... ... ... ,["XYZ",...,"ABC"],["XYZ",...,"ABC"],["XYZ",...,"ABC"]] I want to read this JSON file line by line as: read first: ["XYZ",...,"ABC"] then: ["XYZ",...,"ABC"] so on:' ... ... ... ["XYZ",...,"ABC"] How do I read a JSON file like this, I know it does not completely look like a

java.util.Scanner jumps over input requests

大兔子大兔子 提交于 2019-12-01 08:10:53
问题 I am trying to receive input using java.util.Scanner : Scanner scanner = new Scanner(System.in); int bla = scanner.nextInt(); String blubb = scanner.nextLine(); But the nextLine() command just gets skipped and an empty string is returned. How can I solve this problem? 回答1: The nextInt method does not consume the new line character after the integer so when you call nextLine it just reads the new line character immediately following the integer. Example If you have this input: 42\n foo\n The

Java Scanner won't “finish” reading input

血红的双手。 提交于 2019-12-01 08:06:02
问题 I've been having trouble using java's Scanner class. I can get it to read my input just fine, but the problem is when I want to output something. Given multiple lines of input, I want to print just ONE line when all the input has been read completely. Here's the code I use for reading input: public static void main(String[] args){ Scanner scanner = new Scanner(System.in); //scanner reads block of input while(scanner.hasNextLine()){ //body of loop goes here String s = scanner.nextLine();