You are viewing our Forum Archives. To view or take place in current topics click here.
[Java] Method not returning true value
Posted:

[Java] Method not returning true valuePosted:

-Deano
  • Spooky Poster
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Hello again programming section members. I am back with another problem.
I am making a little trivia app for a project.
The app gets a random question from my Question class.
It then outputs the question and choices A, B, C and D.
The user should then enter A, B, C or D.
If their entry is the same as the correct answer then increase their total score by 1.
Any help is greatly appreciated and solutions can be +repped for being a top lad.

I'm having trouble with it detecting when an answer is correct. An example of what I mean is [ Register or Signin to view external links. ] .

App.java

import java.util.Scanner;

public class App {
   public static boolean Quiz() {
      Scanner scanner = new Scanner(System.in); // New scanner to capture input
      String response = "";
      
      Question Q = new Question(); // Get random question
      System.out.println(Q.getQuestion()); // Print the question
      System.out.println(" (A) " + Q.getA());
      System.out.println(" (B) " + Q.getB());
      System.out.println(" (C) " + Q.getC());
      System.out.println(" (D) " + Q.getD());
      System.out.println();
      System.out.println("Please enter your choice (A, B, C or D)"); // Prompt user for their answer
      response = scanner.nextLine(); // Capture user input
      System.out.println("Your Answer:    " + response);
      System.out.println("Correct Answer: " + Q.getAnswer());
      scanner.close();
      if (response == Q.getAnswer()) {
         System.out.println("Correct!");
         return true;
      } else {
         System.out.println("Wrong!");
         return false;
      }
   }
   
   public static void Quit(int total, int max) {
      System.out.println("Final score: " + total + "/" + max);
      System.exit(0);
   }
   
   public static void main(String[] args) {
      int total = 0;
      int max = 1;
      for (int i = 0; i < max; i++) {
         boolean answer = Quiz();
         if (answer == true) {
            total++;
         }
      }
      Quit(total, max);
      
   }
}



Question.java

import java.util.Random;

public class Question {
   private String question;
   private String[] choice = new String[4];
   private String answer;
   private Random rand = new Random();
   
// Constructor Methods
   public Question() {
      int randomNumber = rand.nextInt(10); // Generate random int between 0 and 10.
      switch(randomNumber) {
      case 0:
         setQuestion("");
         setA("");
         setB("");
         setC("");
         setD("");
         setAnswer("");
         break;
         
      case 1:
         setQuestion("What does ITP mean?");
         setA("Internet Technology Platforms");
         setB("International Technical Performance");
         setC("Intensive Test Preparation");
         setD("Impossible Theoretical Paraplanerism");
         setAnswer("A");
         break;
      case 2:
         setQuestion("Select the topic that is NOT included in the ITP lesson plan.");
         setA("Marketing");
         setB("Computer Architecture");
         setC("Hardware Components");
         setD("Windows OS");
         setAnswer("A");
         break;
      case 3:
         setQuestion("Select the FALSE statement.");
         setA("Computers are getting slower");
         setB("Computers use memory");
         setC("Computers are getting faster");
         setD("Computers use electricity");
         setAnswer("A");
         break;
      case 4:
         setQuestion("Select the TRUE statement.");
         setA("Passwords are not used at all");
         setB("It is not necessary to remember your passwords");
         setC("It is not necessary to keep your passwords secure");
         setD("Passwords are used for security reasons");
         setAnswer("D");
         break;
      case 5:
         setQuestion("Select the TRUE statement.");
         setA("The University does not montiro student attendance");
         setB("It is not necesarry to bring your student card to your lecture");
         setC("You must swipe your student card at the start of the lecture");
         setD("You can use your student card as an international passport");
         setAnswer("C");
         break;
      case 6:
         setQuestion("Which one is NOT a computer part?.");
         setA("SSD");
         setB("DNA");
         setC("RAM");
         setD("HDD");
         setAnswer("B");
         break;
      case 7:
         this.setQuestion("What is Google Inc.?");
         setA("Google is a German automobile manufacturer");
         setB("Google is a French wine company");
         setC("Google is a mountain in Brazil");
         setD("Google is an American multinational corporation specialising in internet-related services and products");
         setAnswer("D");
         break;
      case 8:
         this.setQuestion("What is the shape of a CD?");
         setA("It is round and flat");
         setB("It is a toroid");
         setC("It is a pyramid");
         setD("It is a cube");
         setAnswer("A");
         break;
      case 9:
         this.setQuestion("Where is the Escape key on a typical keyboard?");
         setA("There is no such key");
         setB("Top left corner");
         setC("Bottom right corner");
         setD("On the back of the keyboard");
         setAnswer("B");
         break;
      case 10:
         this.setQuestion("What is meant by HARDWARE?");
         setA("The physical components of a computer system");
         setB("A monitor, mouse and keyboard");
         setC("Applications on a computer system");
         setD("How durable a material is");
         setAnswer("A");
         break;
      }
   }
   
// Mutator Methods
   private void setQuestion(String strIn) {
      question = strIn;
   }
   
   private void setA(String strIn) {
      choice[0] = strIn;
   }
   
   private void setB(String strIn) {
      choice[1] = strIn;
   }
   
   private void setC(String strIn) {
      choice[2] = strIn;
   }
   
   private void setD(String strIn) {
      choice[3] = strIn;
   }
   
   private void setAnswer(String strIn) {
      answer = strIn;
   }
   
// Accessor Methods
   public String getQuestion() {
      return question;
   }
   
   public String getAnswer() {
      return answer;
   }
   
   public String getA() {
      return choice[0];
   }
   
   public String getB() {
      return choice[1];
   }
   
   public String getC() {
      return choice[2];
   }
   
   public String getD() {
      return choice[3];
   }
}
#2. Posted:
Cyimking
  • E3 2016
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Use the .equals() function to compare strings.

Here's a reference link - [ Register or Signin to view external links. ]
#3. Posted:
Inkzzz
  • TTG Senior
Status: Offline
Joined: Oct 28, 201211Year Member
Posts: 1,165
Reputation Power: 45
Status: Offline
Joined: Oct 28, 201211Year Member
Posts: 1,165
Reputation Power: 45
Also for booleans using == true and == false is incorrect code conventions.
#4. Posted:
Clunge
  • 2 Million
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 1,095
Reputation Power: 202
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 1,095
Reputation Power: 202
I know this is over a week old, but I didn't want people to get incorrect information here as people clearly don't know what they are talking about.

Also for booleans using == true and == false is incorrect code conventions.


This is NOT incorrect code conventions and is very much down to the user what they prefer, comparing booleans using == is perfectly acceptable or the user can simply use the method call or variable itself.

Also, I thought it is worth noting that using static is generally bad practice and if avoidable, avoid it. Speaking of naming conventions, one of your methods is called 'Quiz', if following the 'accepted' naming conventions for Java, use lowercase letters followed by uppercase for succeeding words, such as firstSecond().

On a side note, try to modualrise and generalise your code and make use of constructor appropriately.
#5. Posted:
7en
  • V5 Launch
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Clunge wrote
This is NOT incorrect code conventions and is very much down to the user what they prefer, comparing booleans using == is perfectly acceptable or the user can simply use the method call or variable itself.


The overwhelming majority of programmers would argue that comparing to booleans reduces readability, and in a world where readability / re-usability is absolutely key, the user above is right to discourage bad programming practices.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.