Link to home
Start Free TrialLog in
Avatar of daskino12
daskino12

asked on

Java I/O Problem

Hello,

I am trying to read single data items from a file.

Here is my code:

public static String[] readAllStrings ( String filename )
      throws FileIOException
{
      try {
      String temp = "";
      int j = 0;
      
      FileInputStream fin = new FileInputStream(filename);
      BufferedReader din = new BufferedReader(new InputStreamReader(fin));
      
      //while (din.available() != 0)
      //{
      //      temp.concat(din.readLine());
      //}
      
      String line;
      while((line = din.readLine()) != null)
            temp += line;
      
      StringTokenizer st = new StringTokenizer(temp);
      
      String[] strArray = new String[st.countTokens()];
      
      
      while(st.hasMoreTokens()){
            strArray[j] = st.nextToken();
            j++;
      }
      
      
      }
      catch (IOException e)
      {
      }
      
}

but I always get the following error:

Missing a return statement

Thanks
Avatar of daskino12
daskino12

ASKER

Hello,

This one as well.  I did return "temp2" but I still got the same error "Missing a return statement".

Here is my code:

public static double readOneDouble( String filename )
      throws FileIOException
{
      try {
      String temp;
      double temp2;
      
      FileInputStream fin = new FileInputStream(filename);
      BufferedReader din = new BufferedReader(new InputStreamReader(fin));
      temp = din.readLine();
      
      StringTokenizer st = new StringTokenizer(temp);
      temp2 = Double.parseDouble(st.nextToken());
      
      System.out.print("readOneString" + temp2);
            
      fin.close();
      
      return temp2;
      
      }
      
      catch(IOException e)
      {
      }

}

Thanks
You have your method declared at:
public static String[] readAllStrings

This means that the method must return a String array.

Add the following to the end of the method:
return strArray;
In your second post, the reason you have the error is that if an exception is thrown in the try block, your method wont return anything.

Add the following after your catch block:

catch(IOException e)
{
}

return 0.0;
Your method declares a return type of String[], so you have to return that. To your first method, add:

return strArray;

right before the closing brace.

For the second method, you are not returning anything if you get an IOException. I'd suggest changing it to look like this:

public static double readOneDouble( String filename )
     throws IOException
{
     String temp;
     double temp2;
   
...
     
     return temp2;

}


This way, your method will throw an IOException if there's a problem. The calling method can then decide what to do.
Hello,

For the first post, i added return strArray.  But i got the following error:

cannot resolve symbol
symbol  : variable strArray
location: class hw10
        return strArray;

Here is my code:

public static String[] readAllStrings ( String filename )
      throws FileIOException
{
      try {
      String temp = "";
      int j = 0;
      
      FileInputStream fin = new FileInputStream(filename);
      BufferedReader din = new BufferedReader(new InputStreamReader(fin));
      
      String line;
      while((line = din.readLine()) != null)
            temp += line;
      
      StringTokenizer st = new StringTokenizer(temp);
      
      String[] strArray = new String[st.countTokens()];
      
      
      while(st.hasMoreTokens()){
            strArray[j] = st.nextToken();
            j++;
      }
      
      
      }
      catch (IOException e)
      {
      }
      
      return strArray;
}

Thanks
declare strArray outside the try block.

String[] strArray;

try {
//...
strArray = new String[st.countTokens()];
}
catch (...) {}
return strArray;
Hello,

Now, i got "variable strArray might not have been initialized
        return strArray;"

Here is my code:

public static String[] readAllStrings ( String filename )
      throws FileIOException
{
      String[] strArray;
      
      try {
      String temp = "";
      int j = 0;
      
      FileInputStream fin = new FileInputStream(filename);
      BufferedReader din = new BufferedReader(new InputStreamReader(fin));
      
      String line;
      while((line = din.readLine()) != null)
            temp += line;
      
      StringTokenizer st = new StringTokenizer(temp);
      
      strArray = new String[st.countTokens()];
      
      
      while(st.hasMoreTokens()){
            strArray[j] = st.nextToken();
            j++;
      }
      
      
      }
      catch (IOException e)
      {
      }
      return strArray;      

}

Thanks
Hello,

I am sorry since I have the same errors, i might just ask them here. I got the return problem on the following code:

public static int readOneInt( String filename )
      throws FileIOException
{
      try {
      String temp;
      int temp2;
      
      FileInputStream fin = new FileInputStream(filename);
      BufferedReader din = new BufferedReader(new InputStreamReader(fin));
      temp = din.readLine();
      
      StringTokenizer st = new StringTokenizer(temp);
      temp2 = Integer.parseInt(st.nextToken());
      
      System.out.print("readOneString" + temp2);
            
      fin.close();
      
      return temp2;
      
      }
      catch(IOException e)
      {
      }

      return 0;

}

I was wondering if I just add "return 0" at the end, will this work?

Also for this code:

public static String readOneString( String filename )
      throws FileIOException
{      
      try {
      String temp;
      String temp2;
      
      FileInputStream fin = new FileInputStream(filename);
      BufferedReader din = new BufferedReader(new InputStreamReader(fin));
      
      temp = din.readLine();
      
      StringTokenizer st = new StringTokenizer(temp);
      temp2 = st.nextToken();
      
      System.out.print("readOneString" + temp2);
            
      fin.close();
      
      return temp2;
      
      }
      catch(IOException e)
      {
      }
      
}

I don't know what I can return..

For yoren, I am not quiet sure what you meant.  

Thanks
you can initialize it to null;

String[] strArray = null;

then if the function fails, it returns null.
ASKER CERTIFIED SOLUTION
Avatar of lwinkenb
lwinkenb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,

Got it.  Thanks a lot.

no problem, good luck.