An array of strings from a file?
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;
publicclass 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
publicvoid setSurname(String suname)
{
this.surname=suname;
}
public String getSurname()
{
return surname;
}
publicvoid 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();
}
publicvoid test()
{
for (int i = 0; i < 44; i++)
{
System.out.println(LastName[i]);
}
}
}
Can anyone see why its not reading?
[3026 byte] By [
UFC1a] at [2007-11-27 10:16:34]

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.
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 "
> 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.
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....
UFC1a at 2007-7-28 15:46:22 >

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.