Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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

share|improve this question
    
What problem you are facing, are you getting any Exception.. etc. – Sumit Singh Dec 9 '14 at 10:18
    
it says unreported IOException: must be caught or displayed to be thrown. the error is for the line where i call the write method and pass the arrays. – jeeps95 Dec 9 '14 at 10:19
    
Actually, you are doing file handling where check exception my occur. Java doc says that you have to provide exception handling for checked exception. so put try-catch block where you are initializing the class and performing read/write operation on the file and then check the output and let me know the further issue. – Manoj Sharma Dec 9 '14 at 10:22

3 Answers 3

IOException is a checked Exception and your function write (NameX, ScoresArray, players); is throws IOException. So you should handle it in caller.

try {
   write (NameX, ScoresArray, players); //
}catch(IOException ie){
  // do operation 
}

For more details See: Lesson: Exceptions

share|improve this answer
    
is this how its meant to look? – jeeps95 Dec 9 '14 at 10:30
    
is this how it is meant to look?public static void write () try { write (NameX, ScoresArray, players); // } catch(IOException ie) { 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(); } – jeeps95 Dec 9 '14 at 10:32
    
@jeeps95 No, // do operation means handle the Exception. don't put your code inside catch. – Sumit Singh Dec 9 '14 at 10:36

Well, i hope you are looking for this :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * @author manoj.sharma
 */
public class Test {

    /**
     * @param args the command line arguments
     */

    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();



}  
    public static void main(String[] args) {
        String [] names={"manoj","kushal"};
        int [] scores = {10,20};
        int p = 2;
       try{
       write(names,scores,p);
       reads();
      } catch(IOException e){System.out.println(e.getMessage());}

    }

}

I hope you have found your solution.

share|improve this answer
    
hi, thanks for your help but i am still getting same issue – jeeps95 Dec 9 '14 at 10:38
    
please kindly check the edited code i have added above, it is still giving errors – jeeps95 Dec 9 '14 at 10:43
    
Actually, you are getting exception just check my code. I have put colon[:] symbol while writing and i have split string while reading using colon[:] symbol. You are not doing so you first loop cycle read will return output and second cycle will give exception. So you have to modify you code yourself like i have modified to fit your requirement. – Manoj Sharma Dec 9 '14 at 10:43
    
Put your all code. My code is working perfectly on my system and i am getting this output: 2 manoj 10 kushal 20. Without any exception error message. – Manoj Sharma Dec 9 '14 at 10:48

Perhaps you can try using java.lang.Exception rather than IOException, using Exception should cover all types of exceptions that could be thrown.

public static void write (String NameX [], int ScoresArray [], int players ) throws Exception
{
    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();
}

And then use:

try {
   write (NameX, ScoresArray, players); //
}catch(IOException io){
    //Handle an IO exception
}catch(Exception e){
    //Handle Exception
}

I'm afraid I havent had time to test this though.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.