Solved

Java I/O Problem

Posted on 2003-11-02
12
289 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:daskino12
  • 6
  • 5
12 Comments
 

Author Comment

by:daskino12
ID: 9668358
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
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9668398
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;
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9668416
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;
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:yoren
ID: 9668443
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.
0
 

Author Comment

by:daskino12
ID: 9668467
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
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9668498
declare strArray outside the try block.

String[] strArray;

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

Author Comment

by:daskino12
ID: 9668552
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
0
 

Author Comment

by:daskino12
ID: 9668594
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
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9668595
you can initialize it to null;

String[] strArray = null;

then if the function fails, it returns null.
0
 
LVL 5

Accepted Solution

by:
lwinkenb earned 250 total points
ID: 9668600
>>I don't know what I can return..
return null;
0
 

Author Comment

by:daskino12
ID: 9668634
Hello,

Got it.  Thanks a lot.

0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9668637
no problem, good luck.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 18
How do I remove an object from a 3 40
sql import cannot be resolved jsp 3 45
Java Restore security prompts not working 10 56
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question