Results 1 to 12 of 12
  1. #1
    l0afer is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default 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
    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();
    the file is showing like this :
    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 :
    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
    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 ,
    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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();){

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by l0afer View Post
    [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
    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();
    the file is showing like this :
    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 :
    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

    its keeping skipping one postion,i dont know why,any help?
    Try removing your second for loop, as its unneeded and may be causing problems.

  4. #4
    l0afer is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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();){

    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 the
    Java 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

  5. #5
    l0afer is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    Try removing your second for loop, as its unneeded and may be causing problems.
    thanks for your replying ,i tried ,and i got error java.util.NoSuchElementException

    and the output.println(employee.length); is using to check the for loop ,it only show 2 times which is suppose to be 10 ,and the 3rd is running error..

  6. #6
    l0afer is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Java 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]);
           
         }
    }
    }
    thats my full code ....so far
    Last edited by l0afer; 03-28-2011 at 04:45 AM.

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by l0afer View Post
    [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
    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();
    the file is showing like this :
    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 :
    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

    its keeping skipping one postion,i dont know why,any help?
    Quote Originally Posted by l0afer View Post
    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 the
    Java 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
    I see your problem now. There are only 2 commas in the line yet your code is searching for 3. Therefore, since the program can't find a 3rd comma the 3rd value is null.
    Last edited by Solarsonic; 03-28-2011 at 04:43 AM.

  8. #8
    l0afer is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    I see your problem now. There are only 2 commas in the line yet your code is searching for 3. Therefore, since the program can't find a 3rd comma the 3rd value is null.
    i can't find it ,which comma i missed?

  9. #9
    l0afer is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    I see your problem now. There are only 2 commas in the line yet your code is searching for 3. Therefore, since the program can't find a 3rd comma the 3rd value is null.
    if i'm using the for loop which u ask me to delet one can get the full info from the line ,but it keeps skipping the arrary ,it put info in 0 ,not 1,and put in 2,and so on

  10. #10
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by l0afer View Post
    i can't find it ,which comma i missed?
    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.

  11. #11
    l0afer is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    I see your problem now. There are only 2 commas in the line yet your code is searching for 3. Therefore, since the program can't find a 3rd comma the 3rd value is null.
    Quote Originally Posted by Solarsonic View Post
    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.
    thnks anyway,have a good night

  12. #12
    l0afer is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    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.
    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

    Java 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();
          
        }
    thats my new code, and i got the result
    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 code
    Java Code:
     inputFile.nextLine();
    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 got only 1 employee's info ,then get error ,

    i have no idea how this happening .any help?

Similar Threads

  1. how to send scanned in arrary to class?
    By j@v@ in forum New To Java
    Replies: 3
    Last Post: 12-07-2010, 10:06 AM
  2. [SOLVED] Java not fill in all info for text file
    By gotenks05 in forum New To Java
    Replies: 0
    Last Post: 05-24-2009, 05:17 PM
  3. Replies: 9
    Last Post: 09-11-2008, 02:51 AM
  4. How 2d Pixel Arrary Is Stored In .txt/.dat File
    By Mazharul in forum New To Java
    Replies: 1
    Last Post: 08-18-2008, 12:23 PM
  5. Replies: 5
    Last Post: 07-30-2008, 09:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •