java.util.scanner

How to test for blank line with Java Scanner?

久未见 提交于 2019-12-17 10:42:55
问题 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 回答1: 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!"); 回答2: From http:/

How to use .nextInt() and hasNextInt() in a while loop

只愿长相守 提交于 2019-12-17 09:57:38
问题 So I want my program to read an input, which has some integers in one line, for example: 1 1 2 Then it should read every integer separately and print it in a new line. The number of integers the program has to read is not given in advance, so what I am trying to do is use a while loop, which ends after there are no more integers to read. This is the code I wrote: while (scan.hasNextInt()) { int x = scan.nextInt(); System.out.println(x); } but it's not working correctly, because the loop never

Read .txt file into 2D Array

余生颓废 提交于 2019-12-17 06:13:34
问题 There are a few of these topics out there, but this problem has a slight twist that makes it different. I'm focused on only half of a larger problem. I'm sure many of you are aware of the magic square problem. Prompt: Assume a file with lines and numbers on each line like the square shown. Write a program that reads info into a two dimensional array of intS. The program should determine if the matrix is a magic square or not. Working Solution: public static int[][] create2DIntMatrixFromFile

How can i handle it with scanner (java)?

最后都变了- 提交于 2019-12-17 04:07:48
问题 I have a question about scanner please;I working at a small company; we have a software; it generate a big text file; and we must get some useful information from it ; i want write a simple application with java for saving time; could you please guide me ? for example i want this output ; Output RFID : 25 BLUID : 562 WifiID : 2610 RFID : 33 RFID Count : 2 and for example ;this is my Text file, because each generated file with our software has 14000 lines :) --------------------------

Reading a .txt file using Scanner class in Java

人走茶凉 提交于 2019-12-17 01:44:48
问题 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>

Read CSV with Scanner()

坚强是说给别人听的谎言 提交于 2019-12-16 20:56:57
问题 My csv is getting read into the System.out, but I've noticed that any text with a space gets moved into the next line (as a return \n) Here's how my csv starts: first,last,email,address 1, address 2 john,smith,blah@blah.com,123 St. Street, Jane,Smith,blech@blech.com,4455 Roger Cir,apt 2 After running my app, any cell with a space (address 1), gets thrown onto the next line. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class main { public static

Java Scanner - Read line breaks in to a string?

大兔子大兔子 提交于 2019-12-14 04:01:02
问题 I have a scanner that takes user input until ctrl+d and then a while loop which adds each word to a string and then prints it, but i'd like to know how to also include the new line indicators like \n in the string wherever there is a new line. Scanner sc = new Scanner(System.in); System.out.println("Enter your string: "); String x = ""; while (sc.hasNext()){ x+=sc.next() +" "; } System.out.println(x); E.g this code would take the input: Hello Hello Hello and print: Hello Hello Hello Whereas I

Input mismatch error

断了今生、忘了曾经 提交于 2019-12-14 03:59:33
问题 So, I have this little program here and I've been getting the input mismatch error. I'll post the error message below the code. To be honest, I have 0 clue why the error appears. I've checked that all the required variables have correct type assigned. If I understand correctly, the said error happens when you try to input something that is not expected, right? ( double instead of int or whatever). I probably did some stupid mistake somewhere that I cant see for some reason, so I'd appreciate

java: Read text file and store the info in an array using scanner class

只愿长相守 提交于 2019-12-14 00:33:55
问题 I have a text file include Student Grades like: Kim $ 40 $ 45 Jack $ 35 $ 40 I'm trying to read this data from the text file and store the information into an array list using Scanner Class. Could any one guide me to write the code correctly? Code import java.io.*; import java.util.*; public class ReadStudentsGrade { public static void main(String[] args) throws IOException { ArrayList stuRec = new ArrayList(); File file = new File("c:\\StudentGrade.txt"); try { Scanner scanner = new Scanner

How to reset Scanner?

左心房为你撑大大i 提交于 2019-12-14 00:10:26
问题 I want to read a text file and put each line in a String (array of Strings). However that requires scanning file twice, one to figure out how many line are there and another time to create an array of strings of that size. but it throws an error and reset method doesn't seem to work. FileReader read = null; try { read = new FileReader("ModulesIn.txt"); //scan through it and make array of strings - for each line Scanner scan = new Scanner(read); while(scan.hasNextLine()){ numOfMods++; scan