java.util.scanner

Scanner on text file hasNext() is infinite

六眼飞鱼酱① 提交于 2019-11-27 15:43:42
I'm writing a simple program in Java and it requires reading data from a text file. However, I'm having trouble counting lines. The issue seems generic enough for a simple Google search but I may not even be searching the right things. The textbook I'm learning from suggests that to count the number of lines in a text file, you should do something like this: public static int[] sampleDataArray(String inputFile) throws IOException { File file = new File(inputFile); Scanner inFile = new Scanner(file); int count = 0; while (inFile.hasNext()) count++; int[] numbersArray = new int[count]; inFile

How to interrupt java.util.Scanner nextLine call

限于喜欢 提交于 2019-11-27 15:13:22
I am using a multi threaded environment were one Thread is constantly listening for user input by repeatedly calling scanner.nextLine() . To end the application, this runloop is stopped by another thread, but the listening thread won't stop until a last user input was made (due to the blocking nature of nextLine() ). Closing the stream seems not to be an option since I am reading from System.in , which returns an InputStream that is not closable. Is there a way to interrupt the blocking of scanner, so that it will return? thanks This article describes an approach to avoiding blocking when

Java Scanner doesn't wait for user input [duplicate]

你说的曾经没有我的故事 提交于 2019-11-27 15:11:44
This question already has an answer here: Scanner is skipping nextLine() after using next() or nextFoo()? 15 answers I am using Java's Scanner to read user input. If I use nextLine only once, it works OK. With two nextLine first one doesnt wait for user to enter the string(second does). Output: X: Y: (wait for input) My code System.out.print("X: "); x = scanner.nextLine(); System.out.print("Y: "); y = scanner.nextLine(); Any ideas why could this happen? Thanks It's possible that you are calling a method like nextInt() before. Thus a program like this: Scanner scanner = new Scanner(System.in);

Explain this line written in JAVA

*爱你&永不变心* 提交于 2019-11-27 14:13:14
问题 In HACKERRANK this line of code occurs ery frequently. I think this is to skip whitespaces but what does that "\r\u2028\u2029\u0085" thing mean scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 回答1: Scanner.skip skips a input which matches the pattern, here the pattern is :- (\r\n|[\n\r\u2028\u2029\u0085])? ? matches exactly zero or one of the previous character. | Alternative [] Matches single character present in \r matches a carriage return \n newline \u2028 matches the character with

How to determine when end of file has been reached?

送分小仙女□ 提交于 2019-11-27 13:43:06
问题 I am trying to read text from a text file. I need help figuring out when the end of file has occured. How can I determine this in Java? FileInputStream istream = new FileInputStream("\""+filename+"\""); Scanner input = new Scanner(istream); while(EOF != true) { .... } Thanks! 回答1: You can check using hasNextLine() : Scanner input = new Scanner(new File("\""+filename+"\"")); while(input.hasNextLine()) { String data = input.nextLine(); } 回答2: Line based retrieval may be what you want, but token

Issues with nextLine(); [duplicate]

纵饮孤独 提交于 2019-11-27 13:19:25
Possible Duplicate: Scanner issue when using nextLine after nextInt I am trying create a program where it lets the user inputs values into an array using scanner. However, when the program asks for the student's next of kin, it doesn't let the user to input anything and straight away ends the program. Below is the code that I have done: if(index!=-1) { Function.print("Enter full name: "); stdName = input.nextLine(); Function.print("Enter student no.: "); stdNo = input.nextLine(); Function.print("Enter age: "); stdAge = input.nextInt(); Function.print("Enter next of kin: "); stdKin = input

Scanner is never closed

孤人 提交于 2019-11-27 13:09:11
I'm working on a game and I came across a little problem with my scanner. I'm getting a resource leak scanner never closed. But I thought my scanner was working before without closing it. But now it ain't. Anyone can help me out here? import java.util.Scanner; public class Main { public static final boolean CHEAT = true; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amountOfPlayers; do { System.out.print("Select the amount of players (1/2): "); while (!scanner.hasNextInt()) { System.out.println("That's not a number!"); scanner.next(); // this is

Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?

南楼画角 提交于 2019-11-27 12:56:42
How does this program actually work...? import java.util.Scanner; class string { public static void main(String a[]){ int a; String s; Scanner scan = new Scanner(System.in); System.out.println("enter a no"); a = scan.nextInt(); System.out.println("no is ="+a); System.out.println("enter a string"); s = scan.nextLine(); System.out.println("string is="+s); } } The output is: enter the no 1234 no is 1234 enter a string string is= //why is it not allowing me to enter a string here? .nextInt() gets the next int , but doesn't read the new line character. This means that when you ask it to read the

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

穿精又带淫゛_ 提交于 2019-11-27 09:35:18
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 ends, it just wants the user to input more integers. What am I missing here? Your scanner basically

Weird behaviour with Scanner#nextFloat

北城以北 提交于 2019-11-27 09:05:15
Running the following in Eclipse initially caused Scanner to not recognize carriage returns in the console effectively blocking further input: price = sc.nextFloat(); Adding this line before the code causes Scanner to accept 0,23 (french notation) as a float: Locale.setDefault(Locale.US); This is most probably due to regional settings in Windows XP Pro (French/Belgian). When the code is run again 0,23 is still accepted and entering 0.23 causes it to throw a java.util.InputMismatchException . Any explanation as to why this is happening? Also is there a workaround or should I just use Float