Results 1 to 12 of 12
- 03-28-2011, 04:16 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
A problem for put the file info to arrary
[I made a object arrary ,and size is 10. i need to read a file and store the file info to that object arrary,here is my code for reading and storing info to my arrary
the file is showing like this :Java Code:for(i = 0;i < employee.length && inputFile.hasNextLine();i++){ output.println(employee.length); String temp = inputFile.nextLine(); StringTokenizer tem = new StringTokenizer(temp, ","); for(;tem.hasMoreTokens();){ String name = tem.nextToken(); String job = tem.nextToken(); String year = tem.nextToken(); int year1 = Integer.parseInt(year); employee[i] = new Employee (name,job ,year1); } } inputFile.close();
Stewart,Manager,10
Mary,Analyst,6
Tom,Programmer,3
John,Analyst,9
Sylvia,Analyst,5
Jack,Programmer,2
Sue,Programmer,1
Jane,Programmer,6
Rod,Analyst,8
Randy,Programmer,4
and what i got is like :
the info showing in here are right ,it shows name ,job and salary will be show up according the years instead of years number. so, it means the file info stored, but ,its skipping postion to store, it supposed to be countiued ,Java Code:Stewart working as Manager has a salary of $80000 null Mary working as Analyst has a salary of $50000 null Tom working as Programmer has a salary of $30000 null John working as Analyst has a salary of $50000 null Sylvia working as Analyst has a salary of $40000 null
employee[0]=Stewart working as Manager has a salary of $80000
employee[1]=null
employee[2]=Mary working as Analyst has a salary of $50000
.
.
.
.
any one know why???Last edited by l0afer; 03-28-2011 at 06:29 AM.
-
I don't see where you're printing each Employee object to the output file.
And what is the purpose of the inner for loop, this one:
Java Code:for(;tem.hasMoreTokens();){
- 03-28-2011, 04:32 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-28-2011, 04:32 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
thats not my full code ,i just get the problem for putting the file info to arrary ,and i dont need a output file ,the purpose of the inner for loop is i read the info from the file ,and the file is showing above, each line has 3 words, first one is the employee's name ,send one is the job ,3rd is the year, i using the StringTokenizer to split it out ,and using for loop to get each one to put in the Employee arrary ,and theJava Code:Employee(String n, String j, int ye) Constructor for the Employee class
and before the main for loop ,i have a code Employee[] employee = new Employee[size]; size is enter by user
- 03-28-2011, 04:34 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
- 03-28-2011, 04:36 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
thats my full code ....so farJava Code:import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class lab5{ public static void main(String[] args) throws java.io.IOException{ Scanner inputFile = new Scanner(new File("lab5.txt")); Scanner input= new Scanner(System.in); PrintStream output=new PrintStream(System.out); output.print("How many employees do you have: "); int size = input.nextInt(); int i; Employee[] employee = new Employee[size]; for(i = 0;i < employee.length && inputFile.hasNextLine();i++){ output.println(employee.length); String temp = inputFile.nextLine().trim(); StringTokenizer tem = new StringTokenizer(temp, ","); String name = tem.nextToken(); String job = tem.nextToken(); String year = tem.nextToken(); int year1 = Integer.parseInt(year); employee[i] = new Employee (name,job ,year1); } inputFile.close(); for(i=0; i < employee.length; i++){ output.println(employee[i]); } } }Last edited by l0afer; 03-28-2011 at 04:45 AM.
- 03-28-2011, 04:40 AM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-28-2011, 04:44 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
- 03-28-2011, 04:49 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
- 03-28-2011, 04:50 AM #10
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Hey, I gotta go to bed so I can't help you anymore right now.
What I want you to do is write a debug (using System.out.println) and have it print out your year1 variable after your year string is parsed. This will show you directly that its null and then work from there to figure out why its null.
You used your StringTokenizer incorrectly. Also, instead of using a StringTokenizer I would advise you to use the String.split() method found in the Java API.
EDIT: About it putting info starting at 0, arrays start at 0 not 1.Last edited by Solarsonic; 03-28-2011 at 04:56 AM.
- 03-28-2011, 05:03 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
- 03-28-2011, 06:48 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
i tried ur idea, its not common's problem ,the year1 is working fine ,because ,the output shows name ,job and salary ,the salary is denpending on years, its code in class, the output for each one is fine ,the problem is when i enter 10 employees ,i only get 8 of them ,last two i can't read to my arrary
thats my new code, and i got the resultJava Code:for(i = 0;i < employee.length && inputFile.hasNext();i++){ String temp = inputFile.nextLine(); StringTokenizer tem = new StringTokenizer(temp, ","); String name = tem.nextToken(); String job = tem.nextToken(); String year = tem.nextToken(); int year1 = Integer.parseInt(year); employee[i] = new Employee (name,job ,year1); output.println(employee[i]); inputFile.nextLine(); }
Java Code:How many employees do you have: [10] Stewart working as Manager has a salary of $80000 Mary working as Analyst has a salary of $50000 Tom working as Programmer has a salary of $30000 John working as Analyst has a salary of $50000 Sylvia working as Analyst has a salary of $40000 Jack working as Programmer has a salary of $30000 Sue working as Programmer has a salary of $30000 Jane working as Programmer has a salary of $40000 java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at lab5.main(lab5.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
if i delete the last codeJava Code:inputFile.nextLine();
i got only 1 employee's info ,then get error ,Java Code:How many employees do you have: [10] Stewart working as Manager has a salary of $80000 java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at lab5.main(lab5.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
i have no idea how this happening .any help?
Similar Threads
-
how to send scanned in arrary to class?
By j@v@ in forum New To JavaReplies: 3Last Post: 12-07-2010, 10:06 AM -
[SOLVED] Java not fill in all info for text file
By gotenks05 in forum New To JavaReplies: 0Last Post: 05-24-2009, 05:17 PM -
Anyone know how to take info from a txt file and convert it to a char array?
By 2potatocakes in forum New To JavaReplies: 9Last Post: 09-11-2008, 02:51 AM -
How 2d Pixel Arrary Is Stored In .txt/.dat File
By Mazharul in forum New To JavaReplies: 1Last Post: 08-18-2008, 12:23 PM -
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks