java.util.scanner

Scanner method opened and closed twice

筅森魡賤 提交于 2019-11-27 07:16:53
问题 In a single class I have two different methods who are using the scanner class. I created a new instance of scanner object for the first method, then closed it at the end of the first method...then created a new instance of an object (with a different name) in the second method to finally close it at the end of this method. Unless I open the scanner once and close it once it won't work and return an error. The double use of the scanner class doesn't seem to work. What am I doing wrong? Here

Why is Scanner slower than BufferedReader when reading from input?

三世轮回 提交于 2019-11-27 06:45:09
问题 I understand what is Scanner good for, and also when to use Scanner and when BufferedReader. I read a different, yet in some therm similar question Scanner vs. BufferedReader Why is Scanner so slow when I read from the input? I assume it has to do with that there is a small buffer in Scanner, but here I am lost. The original problem is from, Codechef , but I am not interested in that solution. Here is a code example with a given input: Input: 7 3 1 51 966369 7 9 999996 1 And the code import

Scanner skipping every second line from file [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-27 05:39:58
This question already has an answer here: Scanner is skipping nextLine() after using next() or nextFoo()? 15 answers How to use java.util.Scanner to correctly read user input from System.in and act on it? 1 answer I'm trying to scan a text file and place each line into an arraylist, and stop scanning when the next line is a '*', however, my arraylist is storing every 2nd line and I am not sure why. Scanner scan = new Scanner(new File("src/p/input2.txt")); ArrayList<String> words = new ArrayList<String>(); while(scan.hasNextLine()) { if(scan.nextLine().equals("*")) break; words.add(scan

Java using scanner enter key pressed

你离开我真会死。 提交于 2019-11-27 05:33:59
I am programming using Java. I am trying write code which can recognize if the user presses the enter key in a console based program. How can I do this using java. I have been told that this can be done using either Scanner or, buffered input reader. I do not understand(or know how to use) buffered input reader. I tried to do do this using scanner but after pressing enter twice the program terminates, and it doesn't work Scanner readinput = new Scanner(System.in); String enterkey = "Hola"; System.out.print(enterkey); enterkey = readinput.nextLine(); System.out.print(enterkey); if(enterkey == "

Exception in thread “main” java.io.FileNotFoundException: Error

佐手、 提交于 2019-11-27 05:27:23
I am using Eclipse to compile and run my java codes. Here is Error I am getting. Exception in thread "main" java.io.FileNotFoundException: file.txt (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>(Unknown Source) at helloworld.main(helloworld.java:9) Here is my Code import java.io.File; import java.io.IOException; import java.util.Scanner; public class helloworld { public static void main(String[] args) throws IOException { Scanner KB = new Scanner(new File("file.txt"));

Java scanner not going through entire file

拟墨画扇 提交于 2019-11-27 05:01:40
I'm writing a program in Java and one of the things that I need to do is to create a set of every valid location for a shortest path problem. The locations are defined in a .txt file that follows a strict pattern (one entry per line, no extra whitespace) and is perfect for using .nextLine to get the data. My problem is that 241 lines into the file (out of 432) the scanner stops working 3/4 of the way through an entry and doesn't recognize any new lines. My code: //initialize state space private static Set<String> posible(String posLoc) throws FileNotFoundException { Scanner s = new Scanner(new

Java's Scanner vs String.split() vs StringTokenizer; which should I use?

 ̄綄美尐妖づ 提交于 2019-11-27 04:31:58
问题 I am currently using split() to scan through a file where each line has number of strings delimited by '~' . I read somewhere that Scanner could do a better job with a long file, performance-wise, so I thought about checking it out. My question is: Would I have to create two instances of Scanner ? That is, one to read a line and another one based on the line to get tokens for a delimiter? If I have to do so, I doubt if I would get any advantage from using it. Maybe I am missing something here

Java Scanner class reading strings

╄→гoц情女王★ 提交于 2019-11-27 04:27:10
问题 I got the following code: int nnames; String names[]; System.out.print("How many names are you going to save: "); Scanner in = new Scanner(System.in); nnames = in.nextInt(); names = new String[nnames]; for (int i = 0; i < names.length; i++){ System.out.print("Type a name: "); names[i] = in.nextLine(); } And the output for that code is the following: How many names are you going to save:3 Type a name: Type a name: John Doe Type a name: John Lennon Notice how it skipped the first name entry??

Java Scanner String input

空扰寡人 提交于 2019-11-27 04:25:22
问题 I'm writing a program that uses an Event class, which has in it an instance of a calendar, and a description of type String. The method to create an event uses a Scanner to take in a month, day, year, hour, minute, and a description. The problem I'm having is that the Scanner.next() method only returns the first word before a space. So if the input is "My Birthday", the description of that instance of an Event is simply "My". I did some research and found that people used Scanner.nextLine()

Can't use Scanner.nextInt() and Scanner.nextLine() together [duplicate]

我的未来我决定 提交于 2019-11-27 04:16:54
This question already has an answer here: Scanner is skipping nextLine() after using next() or nextFoo()? 15 answers I have to get a string input and an integer input, but there order of input should be that integer comes first then user should be asked for string input Scanner in = new Scanner(System.in); input = in.nextLine(); k = in.nextInt(); in.close(); The above code works fine but if I take an integer input first like in the following code Scanner in = new Scanner(System.in); k = in.nextInt(); input = in.nextLine(); in.close(); then it throws the java.lang.ArrayIndexOutOfBoundsException