-2

This code is encountering a return error. I would appreciate being told how to fix it and more importantly than the former explain why it is necessary to do so. My professor did a pretty shotty job of explaining how a lot of things worked so now I feel like I need to learn a lot that should be known already. Thank you everyone!

import java.io.*;               //Imports any file operation (ie Reading or Writing)
import java.util.Scanner;       //Imports scanner class
import javax.swing.JOptionPane; //Import JOptionPane to allow dialog boxes

public class program7
{
    public String MakeFile() throws IOException
    {
        String NameofDataFile, inputted_text, temp, e;

        temp = "";

        NameofDataFile=JOptionPane.showInputDialog("Enter the name of the file to be opened: ");    //creates file with entered name

        /*allows file to be written in*/
        PrintWriter FileObj = new PrintWriter (new FileWriter (NameofDataFile));

        inputted_text=JOptionPane.showInputDialog("Enter a String: ");                                    //asks user for a string
        e = inputted_text;

        while (e == temp)

            return null;
    }

}
user2864740
  • 60,010
  • 15
  • 145
  • 220
Matthew Osman
  • 85
  • 1
  • 2
  • 8
  • 3
    What if `e` doesn't `== temp`? It won't. Read: [how to compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Sotirios Delimanolis Jul 26 '14 at 02:44
  • and also you might meant there `equals()` – jmj Jul 26 '14 at 02:45
  • the program is designed to keep running until they enter click ok on the input dialog box without entering a value. Then all strings are ready to be viewed in the .dat or .text file.....at least I hope lol – Matthew Osman Jul 26 '14 at 02:45
  • 2
    You really don't need a profs help for this. In the above code, the return is executed only if you enter the while loop. If the code does not enter the while loop, then you are not returning anything for that case. Btw, why don't you always put the code for a while loop inside `{}` ? I don't see how a prof could have explained this badly. – james Jul 26 '14 at 02:46
  • that code will always return NULL – Bourkadi Jul 26 '14 at 02:46
  • he didnt explain that badly because he never really covered it with us. He kind of handed out sheets and said copy the sheet and then boom we got our grades back. I want to go back and redo a lot of the programs now on my own so I was not just copying his work – Matthew Osman Jul 26 '14 at 02:47
  • 2
    Off-topic, but you should know: Java classes should be capitalised as `UppercaseStartingCamelCase`. Variable names should be capitalised as `lowercaseStartingCamelCase`. So your class should be `Program7`, the method should be `makeFile()` and your variables should be `nameOfDataFile`, `fileObj` and `inputtedText`. However, how about we just call them `dataFileName`, `file` and `input` (more succinct, less redundancy). – Greg Kopff Jul 26 '14 at 02:51
  • 1
    http://stackoverflow.com/questions/16789832/missing-return-statement-in-a-non-void-method-compiles – user2864740 Jul 26 '14 at 03:02
  • 1
    possible duplicate of [Return statement within if() for() while()](http://stackoverflow.com/questions/23058029/return-statement-within-if-for-while) – user2864740 Jul 26 '14 at 03:03
  • Also off topic: the word is "shoddy", not "shotty". – David Conrad Jul 26 '14 at 03:08
  • 1
    Matthew - if this is not your first programming course, then handing out a sheet of the basic constructs is just fine. Your lecturer is assuming that you actually learned how things like "while" and "return" worked in some other language ... and the difference in Java is mostly syntax. And he is entitled to assume that you have developed skills for self learning. Like reading a textbook, looking for a web tutorial, etc. You won't get much sympathy here by being rude about your teachers. – Stephen C Jul 26 '14 at 03:11

3 Answers3

1

If e is not equal to temp, then there will be no return statement. Also you may want to use if because while is used for a loop. But as far as you wrote, this is not a loop. Program will return immediately after it enters while. Or maybe your code is not finished and you want to put something inside while. Then you should add a {} bracket block after while.

while(e.equals(temp)) {
// do something
}
return null; // maybe you shouldn't return null. You should return a String
zyl1024
  • 690
  • 1
  • 9
  • 23
0

This statement

 while (e == temp)
   return null;

Will return null if (and only if) e has reference identity with temp. So, you should use equals. Finally you need to return something if that loop is never entered (a valid path as far as the JRE is concerned) -

 if (e.equals(temp)) {
   // if e is equal to temp, no need for a while as far as I see.
   return null;
 }
 return e;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You need to make sure that regardless of what happens in your code, you return something. If you have a conditional statement (if) or a loop (for or while), you need to make sure that you have a return statement for the case that the condditional block or loop never executes.

For example:

public int example(int n){
    while (n > 0)
         return n;
    //what happens if n is <= 0?
}
Nick Meyer
  • 1,771
  • 1
  • 17
  • 29