Jump to content

Java - Input from file

Tohrchur

So I'm trying to get ahead in my labs for my programming class. I've got this one lab almost done but she added a part where I have to take integers from a txt file "input.txt" and have them in an array in the program, then methods with take over.  I've done a whole bunch of Googling but couldn't quite figure out how to get input from a file.  Also it needs input validation to make sure that the only thing being passed to the array are integers.

Any help is greatly appreciated.

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

So I would use a scanner that accesses the .txt. A counter to count .nextInt to get the length of .txt. Then use that for the "for loop" to use the scanner which will grab only ints as that will be required in an "if" statement. 

 

But I think I may be making it harder, as you can just scan   "scanner.nextInt" and grab the int. Like this page explains. 

http://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java

 

 

Spoiler

Corsair 400C- Intel i7 6700- Gigabyte Gaming 6- GTX 1080 Founders Ed. - Intel 530 120GB + 2xWD 1TB + Adata 610 256GB- 16GB 2400MHz G.Skill- Evga G2 650 PSU- Corsair H110- ASUS PB278Q- Dell u2412m- Logitech G710+ - Logitech g700 - Sennheiser PC350 SE/598se


Is it just me or is Grammar slowly becoming extinct on LTT? 

 

Link to comment
Share on other sites

Link to post
Share on other sites

27 minutes ago, Imabigmac said:

So I would use a scanner that accesses the .txt. A counter to count .nextInt to get the length of .txt. Then use that for the "for loop" to use the scanner which will grab only ints as that will be required in an "if" statement. 

 

But I think I may be making it harder, as you can just scan   "scanner.nextInt" and grab the int. Like this page explains. 

http://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java

 

 

Yes, the #nextInt method is the most practical way to get the next integer. Scanner not only has #nextInt, but also the boolean-returning method #hasNextInt. If you look at Scanner's JavaDoc, you can see that #hasNextInt checks if the next token is an integer. 

 

A lot of classes have this format with both a "getter" method to get a certain next value and a condition-checking method to make sure that what you're looking for exists. 

Link to comment
Share on other sites

Link to post
Share on other sites

I always get an exception error message with the 

Scanner sc = new Scanner(file);

do i absolutely have to do a catch and try to work with files? thats what Ive seen from stackoverflow but just seems weird to me that the only way to read from a file is with catch and try.

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

58 minutes ago, Tohrchur said:

I always get an exception error message with the 


Scanner sc = new Scanner(file);

do i absolutely have to do a catch and try to work with files? thats what Ive seen from stackoverflow but just seems weird to me that the only way to read from a file is with catch and try.

Unless you want to make that method throw IOExceptions (and any other method that calls this method, you really shouldn't do this), then yes, you need to use a try and catch. This is how Java handles possible errors (such as the file not existing) and is the proper way to do things. 

You should actually be using a try-with-resources though so that it handles the opening and closing of the scanner. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

I've always found BufferedReader the easiest to use when reading files.

 

BufferedReader br = new BufferedReader(new FileReader(new File("FILENAME.txt")));

You will need to try-catch this statement. Use .readLine() to get the next line of the file. If you have 1 integer per line, you could use the following method:

 

LinkedList<Integer> data = new LinkedList<>();

String line;

while((line = br.readLine()) != null)
{
  data.add(Integer.parseInt(line));
}

This uses a Linked List instead of an array because you don't know how long the file is going to be, so you can't declare the length of the array. If you want to convert the Linked List to an array afterwards, you can do by doing this:

 

int[] arr = new int[data.size()];

for(int i=0;i<data.size();i++)
{
  arr[i] = data.get(i);
}

And of course, don't forget to close your Buffered Reader at the end using

br.close();

Feel free to ask if there's anything here that you don't understand.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Koolwaterstof said:

I've always found BufferedReader the easiest to use when reading files.

 


BufferedReader br = new BufferedReader(new FileReader(new File("FILENAME.txt")));

You will need to try-catch this statement. Use .readLine() to get the next line of the file. If you have 1 integer per line, you could use the following method:

 


LinkedList<Integer> data = new LinkedList<>();

String line;

while((line = br.readLine()) != null)
{
  data.add(Integer.parseInt(line));
}

This uses a Linked List instead of an array because you don't know how long the file is going to be, so you can't declare the length of the array. If you want to convert the Linked List to an array afterwards, you can do by doing this:

 


int[] arr = new int[data.size()];

for(int i=0;i<data.size();i++)
{
  arr[i] = data.get(i);
}

And of course, don't forget to close your Buffered Reader at the end using


br.close();

Feel free to ask if there's anything here that you don't understand.

Why would you suggest the use of a LinkedList over an ArrayList? What factors would you consider when making this decision?

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, Blade of Grass said:

Why would you suggest the use of a LinkedList over an ArrayList? What factors would you consider when making this decision?

Purely because I've never used an ArrayList before, so that's not something that crossed my mind. I said that a LinkedList would be more appropriate than an array, not an ArrayList - you could use an ArrayList instead if you prefer.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×