Using scanner to read a file


















Connect and share knowledge within a single location that is structured and easy to search. 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 have a copy of the. I am using Eclipse by the way. Here's another way to read entire file without loop using Scanner class. Make sure the filename is correct proper capitalisation, matching extension etc - as already suggested. Use the Class. No one seems to have addressed the fact that your not entering anything into an array at all.

You are setting each int that is read to "i" and then outputting it. File Path Seems to be an issue here please make sure that file exists in the correct directory or give the absolute path to make sure that you are pointing to a correct file.

Please log the file. You can change either one to match so that they line up, but just be sure they do. It may help to show the file extensions in whatever OS you're using. Also, for file location, it must be located in the working directory same level as the final executable the.

At first check the file address, it must be beside your. When you check this then try below. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? In this, the scanner class breaks the input of the file into tokens. As a result, we get the output as the tokens into various types using methods such as nextLine , hasnextLine etc. The constructors that can be invoked for java read class are: FileReader String file : This constructor opens the file in the read mode and fetches filename in the string.

The system throws an exception FileNotFound in case the user cannot find the file, or if the file is missing. FileReader File file : This will open the file in the read mode and fetches name of the file in the file instance. In this similar way to Scanner class, the user can also use BufferReader to read the text of the file line by line. In this method, the user can read the text from character-input stream. The user performs the buffer process again and again for better and efficient results in reading characters or arrays.

March 21, at am. Paulous Kunda says:. November 19, at am. Pankaj says:. November 19, at pm. June 27, at am. Harish says:. April 12, at pm. June 16, at am. Joe says:. Suresh says:. October 5, at am. But it also has a constructor which takes File as argument and also has methods hasNextLine and nextLine to find if there is another line of input and reading the line from input respectively.

Using that constructor you can read a file in Java using Scanner. One other benefit of using Scanner is that it has a useDelimiter method, using this method file delimiter can be set thus making Scanner a good choice for reading and parsing CSV, tab delimited or pipe symbol separated files in Java. In the example a File instance is created by passing the file name file which has to be read as argument.



0コメント

  • 1000 / 1000