Results 1 to 7 of 7
Thread: passing a .txt into an array
- 02-07-2011, 04:37 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
passing a .txt into an array
i am having issues. i don't know what to do or what is wrong. any advice?
Java Code:package people; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author Jarrod Dowell */ public class Main { final static int LIMIT = 20; public static void main(String[] args) throws FileNotFoundException{ final String FILENAME = "applicants.txt"; File inputFile = new File(FILENAME); if(inputFile.exists()){ System.out.println("By Jove! The file " + FILENAME + " does exists!"); } else{ System.out.println("Bloody Hell! The file " + FILENAME + " is missing!"); System.exit(1); } Person[] applicants = new Person[LIMIT]; Scanner fileScan= new Scanner(inputFile); int count = 0; int age; for(int i = 0; i < LIMIT; i++){ while(fileScan.hasNext()){ age = fileScan.nextInt(); count++; } } fileScan.close(); fileScan= new Scanner(inputFile); int ages[] = new int[count]; for(int j = 0; j < LIMIT; j++){ if(fileScan.hasNext()){ String[] input = Scanner.nextLine().split(""); applicants[j] = new Person(input[0], Integer.parseInt(input[1])); } } System.out.println("Starting..."); printListString(applicants, LIMIT); System.out.println(getAVG(applicants, LIMIT)); System.out.println(getMedian(applicants, LIMIT)); System.out.println("...Finished"); } private static double getAVG(Person[] ages, int n){ double sum = 0; double avg; for(int i = 0; i < ages.length; i++){ sum = sum + ages[i]; } avg = sum/n; return avg; } private static double getMedian(Person[] ages, int n){ double middle; n = 0; middle = n/2; return middle; } private static void printListString(Person[] list, int n){ for(int i = 0; i < n; i++){ System.out.println("["+i+"] "+ list[i]); } } private static void printList(Person[] list, int n){ for(int i = 0 ; i < n; i++){ System.out.println(list[i]); } } }
- 02-07-2011, 04:39 PM #2
You don't tell us what you want the code to do, what you expect it to do, or what it does. You don't tell us whether you're seeing unexpected behavior, exceptions, or something else.
Read the link in my signature on asking smart questions, then ask a specific question, and we'll go from there.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-07-2011, 07:14 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
ok. i am writing a program to read strings(name and age) into an array of objects. i need the program to read in the info (this is where i am stuck) then sort, average the ages, and find the median age. i cannot figure out how to do this and i am only frustrating myself by not understanding it. please advise.
- 02-07-2011, 07:35 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
What is the format of text file?
What is wrong when you reading it?
Maybe if you are not debugging your code with debugger, you can put more sysouts and find out where the problem is.
- 02-07-2011, 08:22 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
the format of data on the .txt is string name and int age.
- 02-07-2011, 08:47 PM #6
Are you using delimiters? spaces? new lines? can you give us an example of the text file?
- 02-07-2011, 11:45 PM #7
there are at least 3 problems with this code.Java Code:for(int i = 0; i < LIMIT; i++){ while(fileScan.hasNext()){ age = fileScan.nextInt(); count++; } }
1. The outer for loop iterates 20 times, i = 0 to 19. Inner while loop iterates until input is exhausted. So after one iteration of the outer for loop, the inner while loop exhausts input. then the outer for loop iterates for the second time but since the input is exhausted the inner while loop does not and it goes back to the outer for loop which iterates for the third time and the inner while loop doesn't because input is still exhausted, etc etc etc.
2. You mentioned that the format of the text file is String name int age but all you code does is read an int. So when it reads the name and it is not an int it throws an exception.
3. If all above is fixed and the code does actually read the age you do nothing with it. Read the first int and store in the age variable. Read the next int, throw away the first int and replace it with the second int. Read the next int, throw away the second int and replace it with the third int, etc etc etc
Similar Threads
-
Passing Data From One Array To Another
By Sly Cooper in forum New To JavaReplies: 17Last Post: 01-24-2011, 06:07 AM -
Passing an array to a method.
By twcast in forum New To JavaReplies: 9Last Post: 02-10-2010, 09:13 AM -
Passing a 2D array
By toymachiner62 in forum New To JavaReplies: 6Last Post: 10-23-2009, 04:05 PM -
passing an array between classes
By gisler in forum New To JavaReplies: 10Last Post: 04-10-2009, 10:31 PM -
array passing dynamically
By jazz2k8 in forum Advanced JavaReplies: 2Last Post: 10-16-2008, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks