Results 1 to 3 of 3
  1. #1
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default from txt into an array

    My problem (I think) starts after line 41 (code before that is for another requirement). I need to read strings from a file "classpeeps.txt". and then store it in an array. Then I print each line.. I can't use Arraylist or import any other utils.. thanks in advance for any help.

    Java Code:
    //load Scanner and File & IOException
    import java.util.Scanner;
    import java.io.*;
    
    
    public class people 
    {
      public static void main (String [] args) 
      {      
        //delcare a scanner & attach it to kbrd
        Scanner kbrd = new Scanner(System.in);
        
        //prompt user for correct file until found
        boolean found = false;
    
        while (!found)
              {
               try 
                  {
                   System.out.println("Enter the name of the file containing class mates.");
                   File file = new File(kbrd.nextLine());
                   kbrd =  new Scanner(file);
                   found = true;
                  } 
               catch (Exception f) 
                     {
                      System.out.println("File not found. Try again.");
                     }
              }
        //count lines to determine length for arrays
        int lineCounter = 0;
    
        while (kbrd.hasNext())
              {
               String lastName = kbrd.nextLine();
               lineCounter++;
               System.out.println(lastName);
              }
        kbrd.close();
         
        Scanner inputFile = new Scanner ("classpeeps.txt"); 
        
        String[] classMates = new String[lineCounter];
        
        for (int i = 0; i < lineCounter; i++)
            {
             if (inputFile.hasNext())
                {
                 String lastName = inputFile.nextLine();
                   classMates[i] = lastName;
                   
                }
             System.out.println(classMates[i]);
            }
      
        inputFile.close();
    
        
      } 
       
    }

  2. #2
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: from txt into an array

    Failed to mention that the code runs but when it gets time to print the array, it prints NULL as many times as lineCounter.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: from txt into an array

    Your code does not make sense. You apparently prompt for an input file name and when you get it, you exit the loop and then try to count the lines to allocate an array. But then you hard code the file name. Is this a different file? Why don't you just use the file name from the earlier prompt? Also notice the following code:

    Java Code:
    for (int i = 0; i < lineCounter; i++) {
        if (inputFile.hasNext()) {
                 String lastName = inputFile.nextLine();
                  classMates[i] = lastName;  
        }
        System.out.println(classMates[i]);
    }
    Your for loop iterates over lineCounter but you are using inputFile.hasNext() to determine if the line exists. But if the files are the same, they must exist. If they are not the same, you still try and print out something even if hasNext fails. I suspect classpeeps.txt either didn't open up properly or is empty.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 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
  •