Solved

Java I/O Problem

Posted on 2003-11-02
12
262 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now