Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By liv2dtumm2

Thread: New to Java and need assistance please with an error.

  1. #1
    liv2dtumm2 is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default New to Java and need assistance please with an error.

    Hi all,

    I have been taking java for a few weeks now - I am receiving this error below and can't seem to figure out how to correct. Can anyone assist please and thanks.

    Error = HorticultureQuiz.java:88: error: missing return statement
    }
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.

    Program is below:
    Java Code:
    import java.util.Scanner;
    public class HorticultureQuiz
    {
       static String studentName;
       static String studentMajor;
       static String studentConfidence;
       static String newQuesArray;
       static String newAnsArray;
       static int correct = 0;
       static int i;
       static String result; 
       static final String HIGH = "High";
       static final String MEDIUM = "Medium";
       static final String LOW = "Low";
       static final String ERROR = "Incorrect entry";
       public static void main(String[] args)
       {
          String[] questions = {"Plants take in oxygen and emit carbon dioxide.", "The tomato is a vegetable.", 
             "The definition of Horticulture is 'The art or practice of garden cultivation and management,'"};
     
          String[] answers = {"False",  "False", "True"};  
          String studentName = getName();
          System.out.println("Your name is " + studentName);
     
          String studentMajor = getMajor();
          System.out.println("Your major is " + studentMajor);
       
       
          String studentConfidence = getConfidence();
          System.out.println("Your confidence level prior to the quiz is " + studentConfidence);
          
       Scanner keyboard = new Scanner(System.in);
       for(int i = 0; i< 5; ++i)
       {
          System.out.println(questions[i]);
          result = keyboard.nextLine();
          if(result == answers[i])
          {
             System.out.println("Good job!");
             correct = correct + 1;
          }
             else
             {
                System.out.println("Sorry, better luck with next question.");
             }
          
       }
       System.out.println("you have gotten " + correct + "answers out of 5 correct!");
    
       }
       public static String getName()
       {
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Please enter your first and last name " + studentName);
             studentName = keyboard.nextLine();
             return studentName;   
       }
       public static String getMajor()
       {
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Please enter your college major " + studentMajor);
             studentMajor = keyboard.nextLine();
             return studentMajor;   
       }
       public static String getConfidence()
       {  
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Please enter your confidence level prior to taking this quiz " +
             "Enter 1 for high confidence, enetr 2 for medium confidence, enter 3 for low confidence.");
             studentConfidence = keyboard.nextLine();
          if(studentConfidence == "1")
          {
             return HIGH;
          }
             if(studentConfidence == "2")
             {
                return MEDIUM;
             }
                if(studentConfidence == "3")
                {
                   return LOW;
                }
                   if(studentConfidence != "1" && studentConfidence !="2" && studentConfidence != "3")
                   {
                      return ERROR;
                   }
      } 
    }
    Last edited by JosAH; 02-19-2014 at 06:47 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,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: New to Java and need assistance please with an error.

    Look at your last method as a compiler would do: it sees 'if some condition, return something'; three times; next it starts to reason: but what if none of those conditions is true? That method won't return a String value, and whammo: there's your error message. A compiler doesn't interpret your code, it just compiles it; it can't know what your intentions are. The solution is simple though: remove that last if statement and simply return the 'ERROR' value.

    Note that there are more errors in your program. but they'll show up when you attempt to run it.

    kind regards,

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

  3. #3
    ankurt is offline Member
    Join Date
    Feb 2014
    Posts
    11
    Rep Power
    0

    Default Re: New to Java and need assistance please with an error.

    Have a part of the code that runs when all the listed conditions fail, and return a string in that. It will run fine. And as JosAH says, there are more errors in your program, which you will come across when you run.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: New to Java and need assistance please with an error.

    Quote Originally Posted by ankurt View Post
    Have a part of the code that runs when all the listed conditions fail, and return a string in that. It will run fine.
    ... what??
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    ankurt is offline Member
    Join Date
    Feb 2014
    Posts
    11
    Rep Power
    0

    Default Re: New to Java and need assistance please with an error.

    gimbal2
    It was to tell him to have a block of code that gets executed when all the if conditions failed i.e an "else" in his code, and a string should be returned in that part to meet the return type criterion of the method.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: New to Java and need assistance please with an error.

    Quote Originally Posted by ankurt View Post
    gimbal2
    It was to tell him to have a block of code that gets executed when all the if conditions failed i.e an "else" in his code, and a string should be returned in that part to meet the return type criterion of the method.
    JosAH explained that about a hundred times more clearly.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    ankurt is offline Member
    Join Date
    Feb 2014
    Posts
    11
    Rep Power
    0

    Default Re: New to Java and need assistance please with an error.

    Ok. Thanks for your feedback.

  8. #8
    liv2dtumm2 is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Re: New to Java and need assistance please with an error.

    Quote Originally Posted by JosAH View Post
    Look at your last method as a compiler would do: it sees 'if some condition, return something'; three times; next it starts to reason: but what if none of those conditions is true? That method won't return a String value, and whammo: there's your error message. A compiler doesn't interpret your code, it just compiles it; it can't know what your intentions are. The solution is simple though: remove that last if statement and simply return the 'ERROR' value.

    Note that there are more errors in your program. but they'll show up when you attempt to run it.

    kind regards,

    Jos
    Thank you. I got rid of that last if statement and just put in else and the program compiles. But you were right, I have more issues. This is my 6th week of online Java and my first course in programming. I am pretty confused. The program runs but the if statements don't work correctly and all the questions receive the else statement, even when correctly answered? I appreciate the responses and can use all the help I can get.
    Thank you guys!
    liv2drumm
    ankurt likes this.

Similar Threads

  1. Some java assistance
    By trtmerlin in forum New To Java
    Replies: 1
    Last Post: 04-01-2012, 12:37 PM
  2. assistance w/ java programming assignment
    By clemsontigers in forum New To Java
    Replies: 4
    Last Post: 04-07-2011, 08:07 PM
  3. Learning Generics in Java. Need Assistance
    By codex in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 05:00 PM
  4. error message assistance
    By Sean_J in forum New To Java
    Replies: 12
    Last Post: 03-17-2010, 02:27 AM
  5. Error assistance
    By bobbychiken in forum New To Java
    Replies: 2
    Last Post: 11-21-2009, 10:54 PM

Tags for this Thread

Posting Permissions

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