Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Can't figure out

  1. #1
    beauti477 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default Can't figure out

    I have stared at this soo long my eyes have crossed, I can't figure out what the issue is.
    I am getting this error message:

    MyMortCalc.java:32: missing return statement
    }
    ^
    1 error

    Any ideas??? What I am looking for is for the program to ask the inputter for a password and if they give the wrong password three times the program stops, and if they give the correct password the program would start with the mortgage question.
    Java Code:
    import java.text.*;
    import java.io.*; 
      
    public class MyMortCalc
    {
      static String getPassword() throws Exception
      {
        BufferedReader cin; 
        cin = new BufferedReader(new InputStreamReader(System.in));
    
        int i;
        i = 0;
    
        while (i < 3)
        {
          String password;
          System.out.print("Enter the password: ");
          password = cin.readLine();
          if (password.equals("1212274")) break;
          else 
            System.out.print("Invalid ");
            System.out.println();
          i = i + 1;
    
          return password;
        }
      } 
    
      public static void main(String[] argv) throws Exception 
      {
        getPassword();
    
          BufferedReader cin; 
          cin = new BufferedReader(new InputStreamReader(System.in));
    
          int years = 30;
    
          // read an int value from the keyboard 
          double mort; 
          System.out.print("What's the mortgage amount? "); 
          mort = new Double(cin.readLine()).doubleValue();
    
          // read an int value from the keyboard 
          double interest; 
          System.out.print("What's the annual interest rate? "); 
          interest = new Double(cin.readLine()).doubleValue();
        
          //output value
          double I = (interest/100) / 12;
          double S = (mort *((Math.pow(1 + (I), (years * 12)) * I))/(Math.pow(1 + I, (years * 12))-1));
    
          // echo output
          System.out.println();
          System.out.println("Mortgage Amount = $" +new DecimalFormat("#").format(mort));
          System.out.println("Annual Interest Rate = " +interest + "%");
          System.out.println("Amorization Period = " +years + " Years");
          System.out.print("This monthly payment will be $" +new DecimalFormat("#000.00").format(S));
          System.out.println();
          System.out.println();
    
          // write output to file mortgages.txt
          PrintWriter fout;
          fout = new PrintWriter(new FileWriter("mortgages.txt", true));
          fout.println("For a loan amount of $" +new DecimalFormat("#").format(mort) + ", at an interest rate of " +interest + "%, the monthly payment is $" +new DecimalFormat("#000.00").format(S) + ".");
          fout.close();
      }
    }
    Last edited by JosAH; 07-20-2011 at 09:03 AM. Reason: Added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    You're telling the compiler that the getPassword() method returns a String. The compiler only sees that it's returning a String inside the body of a while loop; it doesn't do any calculations and reasons: what if that while loop is never entered, then nothing is returned, and it starts whining. The cure is simple: keep the compiler happy by putting a dummy "return null;" as the last statement of your method.

    kind regards,

    Jos
    yellowledbet likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by beauti477 View Post
    Java Code:
      static String getPassword() throws Exception
      {
        BufferedReader cin; 
        cin = new BufferedReader(new InputStreamReader(System.in));
    
        int i;
        i = 0;
    
        while (i < 3)
        {
          String password;
          System.out.print("Enter the password: ");
          password = cin.readLine();
          if (password.equals("1212274")) break;
          else 
            System.out.print("Invalid ");
            System.out.println();
          i = i + 1;
    
          return password;
        }
      }
    Your return statement is in the while loop. The compiler has no guarantee that code in a loop will be executed.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Like JosAH and yellowledbet said, the compiler doesn't know if it will enter to loop because it doesn't think about the code. It only looks to see if under any condition will the code return something. You know that it will always enter the loop, but the compiler doesn't.

    Also your indentation of your else statement is wrong. It will only execute the first line "System.out.print("Invalid ");" if password does not equal 1212274. It will execute "System.out.println();" no matter what. So either your indentation is wrong, or you forgot the brackets.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Cant figure this out
    By Shimless12 in forum New To Java
    Replies: 1
    Last Post: 07-10-2011, 09:48 PM
  2. see if you can figure out this 'anomaly'
    By gib65 in forum Advanced Java
    Replies: 3
    Last Post: 10-02-2010, 04:49 PM
  3. Need help - I can't figure it out.
    By Joshsmith in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 10:12 PM
  4. Need Help in BlueJ... Cannot figure this out.
    By ERICAMAUVE4 in forum New To Java
    Replies: 2
    Last Post: 09-25-2009, 12:34 AM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •