Solved

Java I/O Problem

Posted on 2003-11-02
12
295 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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