?
Solved

Java I/O Problem

Posted on 2003-11-02
12
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 1000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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