6 Replies - 406 Views - Last Post: 02 December 2009 - 08:46 AM Rate Topic: -----

#1 azale187  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 09

Reading a file to an array

Posted 02 December 2009 - 08:19 AM

I am trying to read a txt file with java and put it in an array. I am getting an error on the new Scanner(file) that says unhandled exception type FileNotFoundException.

any help would be greatly appreciated.

package userInterface;
import java.util.*;
import java.io.*;

public class ChargeTest {
	
	public static void main(String[] args)
	{		
		
		final int SIZE = 5;
		int[] numbers = new int[SIZE];
		int index = 0;
		
		File file = new File("Values.txt");
		Scanner inputFile = new Scanner(file);
		
		while (inputFile.hasNext() && index< numbers.length)
		{
			numbers[index] = inputFile.nextInt();
			index++;
		}
		
		inputFile.close();

	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Reading a file to an array

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,311
  • Joined: 27-December 08

Re: Reading a file to an array

Posted 02 December 2009 - 08:21 AM

Your compiler is saying that you do not take into account the possibility that your File may not exist. Try something like this (make sure to import java.io.*;):
Scanner scan = null;
File f = null;
try{
	f = new File("path");
	scan = new Scanner(f);
}
catch(FileNotFoundException e){
	System.out.println("File not found");
	System.out.pritnln("Exiting");
	System.exit(0);
}



What will happen is if your File isn't found, then the program will exit.
Was This Post Helpful? 0
  • +
  • -

#3 azale187  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 09

Re: Reading a file to an array

Posted 02 December 2009 - 08:33 AM

I have the import.java.io.*; at the top of my code. Is there any reason why it won't find my file? I have it in the same folder my code is saved in. I am using IBM rational software architect.

View Postmacosxnerd101, on 2 Dec, 2009 - 07:21 AM, said:

Your compiler is saying that you do not take into account the possibility that your File may not exist. Try something like this (make sure to import java.io.*;):
Scanner scan = null;
File f = null;
try{
	f = new File("path");
	scan = new Scanner(f);
}
catch(FileNotFoundException e){
	System.out.println("File not found");
	System.out.pritnln("Exiting");
	System.exit(0);
}



What will happen is if your File isn't found, then the program will exit.

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,311
  • Joined: 27-December 08

Re: Reading a file to an array

Posted 02 December 2009 - 08:38 AM

Whoops- I forgot that the ; followed by the ) symbols would create an emoticon.

By the "path" I placed in your File constructor, I meant replace that with the actual file path (or if it is in the same folder, the name of the File).
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2832
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Reading a file to an array

Posted 02 December 2009 - 08:41 AM

See

http://technojeeves..../74-string-list
Was This Post Helpful? 0
  • +
  • -

#6 azale187  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 09

Re: Reading a file to an array

Posted 02 December 2009 - 08:43 AM

I tried that now, and when I try to read it, the output is just the filename...
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,311
  • Joined: 27-December 08

Re: Reading a file to an array

Posted 02 December 2009 - 08:46 AM

Please remember to post your revised code each time you describe new errors. Thanks for helping us help you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1