Results 1 to 15 of 15
  1. #1
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default need to input values from a text file into an array and count values

    hi guys im new to java and i need to input int from a text file into an array and count the values together
    heres what i have got so far.
    as you can see from the code below the array is called guestNumbers and the int i want to store is i3 then i want to add the values stored in guestNumbers and then divide the result by the length of guestNumbers


    Java Code:
     Scanner cc = new Scanner( new File("CC.txt") );
              Scanner sn = new Scanner( new File("sn.txt") );
              Scanner ng = new Scanner( new File("gn.txt") );
              String line ;
              String line2 ;
              String line3 ;
              System.out.println("course code"+" " + "Student Number" +" "+"Number of guests");
              int count =1;
               guestNumbers = new double[count];
              while( cc.hasNext() && sn.hasNext() && ng.hasNext() ){
                line = cc.nextLine();
                line2 = sn.nextLine();
                line3 = ng.nextLine();
                
                
                int i=Integer.parseInt(line);
                int i2=Integer.parseInt(line2);
                int i3=Integer.parseInt(line3);
                
                System.out.println(i + "         " + i2 + "         " + i3);
    count++;
     
        
      
            }

  2. #2
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    File fFile = new File("c:\\sld32" + ".txt");
    fr = new int[272];
    try {
    FileInputStream fstream = new FileInputStream(fFile);
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(
    new InputStreamReader(in));
    String strLine;
    // Read File Line By Line
    int i = 0;
    while ((strLine = br.readLine()) != null) {
    // Print the content on the console
    // System.out.println (strLine);
    fr[i] = Integer.parseInt(strLine.trim());
    i++;
    }
    System.out.println("integer conversion done& value r in int");

    }// try end
    catch (Exception e) {// Catch exception if any
    System.err.println("Error: xyz" + e.getMessage());
    }

    i think this will helpful!!!
    Good luck!!

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    you declared
    Java Code:
    guestNumbers = new double[count];
    when the count is 1. means your array is only 1 element? is that really what you want? If possible, use an ArrayList to store your data

  4. #4
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default thanks

    thanks i have altered the code to fit into my program but im just getting 0 from the array what seems to be happening is that the int i istnt coming out of the while loop so i is allways equal to 0 and the array in position 0 is also equal to 0


    Java Code:
                   File fFile = new File("gn.txt");
    int[] fr = new int[272];
    try {
    FileInputStream fstream = new FileInputStream(fFile);
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(
    new InputStreamReader(in));
    String strLine;
    // Read File Line By Line
    int i = 0;
    int t= fr[i];
    while ((strLine = br.readLine()) != null) {
    // Print the content on the console
    // System.out.println (strLine);
    fr[i] = Integer.parseInt(strLine.trim());
    i++;
    
    
    }
    System.out.println("total = "+ t);
    
    }// try end
    catch (Exception e) {// Catch exception if any
    System.err.println("Error: xyz" + e.getMessage());
    }

  5. #5
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    hi javahater what im aiming at is the array guestNumbers to be the length of the number of lines in gn.txt does a list allow this and how would you implement it?

  6. #6
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    why did you change your code to use BufferedReader? Using Scanner is fine.

  7. #7
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by pds8475 View Post
    hi javahater what im aiming at is the array guestNumbers to be the length of the number of lines in gn.txt does a list allow this and how would you implement it?
    I myself would use an ArrayList. However for your case, I am assuming you are doing your homework and can't use ArrayList ?? here are some ways (which I don't like), but since you are learning only....
    1) set an initial count to your array when you declare it
    2) count the number of lines first, then you will know how many integers are there. then you can declare the array with that size. Then go through the file again..... ....

  8. #8
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    well i was just trying to follow the example that baloda as close as posible

  9. #9
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    i take it you meen some thing like this when you say count the number of lines first


    int count=0;
    while(ng.hasNext() ){
    count=count++;
    }
    System.out.println(count);


    my problem is that inside the while loop count may change but outside count stays the same

  10. #10
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    first check if ur file has enough elements i.e elements in more then one line

  11. #11
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    it is at the moment the file has 4 lines users can input new lines and that side of the program is fully working i can output the lines to the screen which i do and that part works
    but i cant get the counter to work to initilise the array guestNumbers

  12. #12
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    as far i can understand u can do like this
    suppose

    fr[i]=aaa;
    then
    guestNumbers [j]= i where guestName is aaa;

  13. #13
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by pds8475 View Post
    i take it you meen some thing like this when you say count the number of lines first


    int count=0;
    while(ng.hasNext() ){
    count=count++;
    }
    System.out.println(count);


    my problem is that inside the while loop count may change but outside count stays the same
    count++ is enough. No need to assign it back to count.
    Java Code:
    int count=0;
     while(ng.hasNext() ){
                count++;
      }
    int guestNumbers = new int[count];
    Last edited by JavaHater; 01-22-2011 at 01:36 PM.

  14. #14
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    right guys got the counter to work thanks now i just need to enter the values into the array

  15. #15
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    right now i have this as you can see im outputting a element from the array to the screen but no matter what element i say to output what shows on screen is 1.0 this is the last element that the array should be holding but inbetween there are supposed to be 2 elements with different numbers

    Java Code:
      Scanner cc = new Scanner( new File("CC.txt") );
              Scanner sn = new Scanner( new File("sn.txt") );
              Scanner ng = new Scanner( new File("gn.txt") );
              String line ;
              String line2 ;
              String line3 ;
              System.out.println("course code"+" " + "Student Number" +" "+"Number of guests");
             
              int lineCount = 0;  
    
     BufferedReader br2 = new BufferedReader(new FileReader("gn.txt"));  
     while ((line = br2.readLine()) != null) {  
     lineCount++;  
     } 
     System.out.println(lineCount);
    double guestNumbers[] = new double[lineCount];
      
                
              while( cc.hasNext() && sn.hasNext() && ng.hasNext() ){
                line = cc.nextLine();
                line2 = sn.nextLine();
                line3 = ng.nextLine();
                
                
                int i=Integer.parseInt(line);
                int i2=Integer.parseInt(line2);
               double i3=Double.parseDouble(line3);
               
                System.out.println(i + "         " + i2 + "         " + i3);
              for(int c=0; c< lineCount; c++)
    {
                guestNumbers[(c)]=i3;
    }                      
        
             }
          
    System.out.println(guestNumbers[0]);
    System.out.println(guestNumbers[1]);
    System.out.println(guestNumbers[2]);
    System.out.println(guestNumbers[3]);
         
       
             cc.close();
            sn.close();
            ng.close();



    my output from the program is

    4
    321 123321 1.0
    321 654123 2.0
    111 652145 3.0
    324 545545 1.0
    1.0
    1.0
    1.0
    1.0

    but should be
    4
    321 123321 1.0
    321 654123 2.0
    111 652145 3.0
    324 545545 1.0
    1.0
    2.0
    3.0
    1.0

Similar Threads

  1. Java 2D array get values from text fields
    By raverz1tildawn in forum Java 2D
    Replies: 3
    Last Post: 01-08-2011, 12:56 AM
  2. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  3. Replies: 40
    Last Post: 07-04-2008, 08:41 AM
  4. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 02:22 PM
  5. [SOLVED] getting values from a text file
    By dav9999 in forum New To Java
    Replies: 8
    Last Post: 04-01-2008, 01:51 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
  •