I am trying to bring 2 arrays and one integer from a different method into the method that writes the file. I tried bringing in using arguments but it is not working. Hope you can help
This is how i passed argument from method holding the arry informations.
write (NameX, ScoresArray, players); /
This is the method that is meant to use the passed array arguments to create file.
public static void write (String NameX [], int ScoresArray [], int players ) throws IOException
{
PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));
outputStream.println(players);
for (int i = 0; i < NameX.length; i++)
{
outputStream.println(NameX[i] + ScoresArray [i] );
}
outputStream.close();
}
This is the method that reads the file
public static void reads () throws IOException
{
BufferedReader inStream = new BufferedReader(new FileReader("Genius.txt"));
// Read in first file entry as an integer - the number of names stored
int players = Integer.parseInt(inStream.readLine());
System.out.println(players);
// Create an array big enough
String [] NameX = new String[players];
String [] ScoresArray = new String[players];
// Now read in the names
for (int i = 0; i < NameX.length; i++)
{
NameX[i] = inStream.readLine();
ScoresArray[i] = inStream.readLine();
System.out.println(NameX[i]);
System.out.println(ScoresArray[i]);
}
inStream.close();
}
Please could you tell me where i am going wrong and how i can create a file that saves the arrays and reads the file later.
************* Edited code********************
write(NameX,ScoresArray,players);
reads();
}
}//end of league table
public static void write (String NameX [], int ScoresArray [], int players ) throws IOException
{
PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));
outputStream.println(players);
for (int i = 0; i < NameX.length; i++)
{
outputStream.println(NameX[i] +":"+ ScoresArray [i] );
}
outputStream.close();
} public static void reads () throws IOException { BufferedReader inStream = new BufferedReader(new FileReader("Genius.txt"));
// Read in first file entry as an integer - the number of names stored
int players = Integer.parseInt(inStream.readLine());
System.out.println(players);
// Create an array big enough
String [] NameX = new String[players];
String [] ScoresArray = new String[players];
// Now read in the names
for (int i = 0; i < NameX.length; i++)
{
String str = inStream.readLine();
String vals[] = str.split(":");
System.out.println(vals[0]+" "+vals[1]);
}
inStream.close();
}
i am still getting same issue