Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
04-18-2013, 12:04 AM #1
- Join Date
- Apr 2013
- Thanked 0 Times in 0 Posts
Reading txt file contents into parallel arrays code not working
// Create File Object
String fileName = "myfile.txt";
File file = new File(fileName);
Scanner scan = new Scanner(file);
int counter = 0;
String code = scan.next();
String moduleName = scan.next();
int totalPurchase = scan.nextInt();
moduleCodes[counter] = code;
moduleNames[counter] = moduleName;
numberOfStudents[counter] = totalPurchase;
The above code doesn't work.
When I try to print the first element of the first array like this:
it doesn't work, it just prints out Null.
Note: The code above for reading the file contents into arrays in inside a method.
04-18-2013, 05:48 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,662 Times in 2,631 Posts
Put an else clause on the
if (file.exists()). Although the scanner is a checked exception when given a file, it won't ever trigger if you use a file.exists() check prior to creating the scanner. Everything else looks correct.PHP Code:
header('HTTP/1.1 420 Enhance Your Calm');