6 Replies Latest reply: Jul 11, 2007 1:01 PM by 807600 RSS

    An array of strings from a file?

    807600
      I have a file with 2000 names and want to make an array of type string................

      When i compile and run all all i get is null values in my array?
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.PrintWriter;
      import java.util.Scanner;
      import java.io.IOException;
      
      public class Names
      {
                private String name;
                private String surname;
                PrintWriter output = null;
                String[] LastName = new String[2000]; //set up array of first names
                String[] Name = new String[2000];; //set up array of first names
      
      
           public void setSurname(String suname)
           {
                this.surname=suname;
           }
      
           public String getSurname()
           {
                return surname;
           }
      
           public void loadFromFile(String fname) //method loads form text file
           {
                File file = new File("SURNAMES.txt");
                Scanner in = null;
                try {
                      in = new Scanner(file);
                       }
                       catch (FileNotFoundException fnfe)
                     {
                          System.out.println("File not found");
                     }
      
                      int x=0;
                      while(in.hasNextLine())
      
                      {
                        surname = in.next();
                          LastName [x] = new String(surname);
                        x++;
                      }
      
                     in.close();
      
           }
      public void test()
           {
                for (int i = 0; i < 44; i++)
                {
                System.out.println(LastName);
                }
           }

      }



      Can anyone see why its not reading?
        • 1. Re: An array of strings from a file?
          807600
          Either step through your code in the debugger, or add some illustrative print statements to see if the code is doing what you expect.
          • 2. Re: An array of strings from a file?
            807600
            Your code should work, at least the lastName array should be filled and display if you call test();

            You should also do this instead of your current approach:
                                  int x=0;
                            while(in.hasNextLine())
                            {
                                LastName [x] = in.next();
                              x++;
                            }
            An ArrayList would have been better, it keeps count for you when you add and it is not of a fixed size so it allows more flexibility, at the cost of some overhead.
            • 3. Re: An array of strings from a file?
              807600
              too many useless thing, such as variables name and surname
              try

              lastName[x] = in.next();

              But this is not the error.
              Maybe in.hasNextLine() returns false...
              try to put a System.out.println in while outputting the names...
              Are you sure SURNAMES.txt exists ?

              and please, read the code convention document.
              "String[] LastName " >> "String[] lastName "
              • 4. Re: An array of strings from a file?
                807600
                too many useless thing, such as variables name and
                surname
                try

                lastName[x] = in.next();

                But this is not the error.
                Maybe in.hasNextLine() returns false...
                try to put a System.out.println in while outputting
                the names...
                Are you sure SURNAMES.txt exists ?

                and please, read the code convention document.
                "String[] LastName " >> "String[] lastName "
                There is no error, with the file in the location stated that code will work. It could do with a makeover but it runs.
                • 5. Re: An array of strings from a file?
                  807600
                  import java.io.File;
                  import java.io.FileNotFoundException;
                  import java.util.Scanner;
                  import java.io.PrintWriter;
                  
                  public class ResultPredictor
                  {
                       public static void main (String args[])
                       {
                            Names play = new Names();
                  
                            //display menu to user & process user choice
                            Scanner input = new Scanner(System.in);
                            int choice = 0;
                            while(choice!=4)
                            {
                                 System.out.println("__________________________________________________________");
                                 System.out.println("");
                                 System.out.println("[1] Join the names");
                                 System.out.println("[2] Display names that both name start with the same letter");
                                 System.out.println("[3] Search for names starting with a letter");
                                 System.out.println("[4] Exit");
                                 System.out.println("");
                                 System.out.print("What is your choice? ");
                                 System.out.flush();
                  
                                 //check and process an integer choice
                                 if (input.hasNextInt())
                                 {
                                      choice=input.nextInt();
                                      switch(choice)
                                      {
                                                case 1:
                                                     play.test();
                                                     break;
                                                case 2:
                  
                                                     break;
                                                case 3:
                  
                                                     break;
                                                case 4:
                                                     System.out.println("\nGOODBYE!!\n\n");
                                                     break;
                  
                                                default:
                                                     System.out.println("Please enter a valid menu option (1-4)");
                                                     System.out.println("");
                                                     break;
                                      }
                                 }
                                 else //choice was not an integer
                                 {
                                      System.out.println("Please enter a valid menu option (1-4)");
                                      input.next(); //move on
                                 }
                  
                            }//end loop
                       }// end main
                  
                  
                  }
                  when i run the code it returns null values, the SURNAMES.txt file is in the same folder. Could it be the way the files is delimited, its like so.........


                  a
                  b
                  c
                  d
                  e
                  f
                  g
                  and so on....
                  • 6. Re: An array of strings from a file?
                    807600
                    Why do you expect play.names() to print anything? All I can see is that you create an instance of the names class through its default constructor and then call the play method, which prints exactly what is in the array, null Strings.

                    You are not calling loadFromFile() anywhere in the code you posted so far and this is why you are not getting the results you expect.

                    Also why have loadFromFiles() take an argument of a String, loadFromFile(String fname)? You are not using fname in that method.