Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Making A Quiz With Loops, Incompatible Type Error

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Making A Quiz With Loops, Incompatible Type Error

    My homework was to create a program that prompts the user with questions.
    They will get 5 attempts, each attempt that they guess incorrectly the program will deduct 5 points until they reach 0.
    Each question is worth 20 points.

    I have made the code as best as I can but for some reason I cannot total all of the scores for each question without getting the "incompatible type" error.

    import java.util.Scanner; // Needed for the Scanner class
     
    /**
       This program demonstrates a case insensitive string comparison.
    */
     
    public class FiveQuestionsII
    {
     
     
     
       public static void main(String[] args)
       {
     
    //QUESTION 1
     
    	int number1 = 20;
    	String total;
    	String ans1, ans2, ans3, ans4, ans5;
    	Scanner keyboard = new Scanner(System.in);
    	int counter = 0;
     
          System.out.print("1. What is each repetition of a loop known as?\nAnswer: ");
          ans1 = keyboard.nextLine();
     
          while(counter < 4 && !ans1.equals("iteration") && !ans1.equals("Iteration")) {
          System.out.print("Incorrect, please try again " + "\nAnswer: ");
    		counter++;
    		number1 = 20 - (counter*5);
     
    		ans1 = keyboard.nextLine();		
    	}
    		System.out.println("You scored " + number1 + " points out of 20 for that question.\n(Answer: iteration)");
     
     
    //QUESTION 2
     
         int number2 = 20;
         System.out.print("2. The while loop is this type of loop...\nAnswer: ");
         ans2 = keyboard.nextLine();
     
         while(counter < 4 && !ans2.equals("pretest") && !ans2.equals("Pretest")) {
         System.out.print("Incorrect, please try again " + "\nAnswer: ");
    		counter++;
    		number2 = 20 - (counter*5);
     
    			ans2 = keyboard.nextLine();		
    	}
    		System.out.println("You scored " + number2 + " points out of 20 for that question.\n(Answer: pretest)");
     
    //QUESTIONS 3
     
         int number3 = 20;
         System.out.print("3. This is a special value that signals when there are no more items from a ist of items to be processed...\nAnswer: ");
         ans3 = keyboard.nextLine();
     
         while(counter < 4 && !ans3.equals("sentinel") && !ans3.equals("Sentinel")) {
         System.out.print("Incorrect, please try again " + "\nAnswer: ");
    		counter++;
    		number3 = 20 - (counter*5);
     
    			ans3 = keyboard.nextLine();		
    	}
    		System.out.println("You scored " + number3 + " points out of 20 for that question.\n(Answer: sentinel)");
     
    //QUESTION 4
     
         int number4 = 20;
         System.out.print("4. This class allows you to read a line from a file... \nAnswer: ");
    	  ans4 = keyboard.nextLine();
     
         while(counter < 4 && !ans4.equals("Scanner") && !ans4.equals("scanner")){    
    		System.out.print("Incorrect, please try again " + "\nAnswer: ");
    			counter++;
    			number4 = 20 - (counter*5);
     
    			ans4 = keyboard.nextLine();		
    			}
    	System.out.println("You scored " + number4 + " points out of 20 for that question.\n(Answer: scanner)");
     
    //QUESTION 5
     
    	  int number5 = 20;
    	  System.out.print("5. This type of loop has no way of ending and repeats until the program is interrupted... \nAnswer: ");
    		  ans5 = keyboard.nextLine();
     
    	  while(counter < 4 && !ans5.equals("Infinite") && !ans5.equals("infinite")){    
    		System.out.print("Incorrect, please try again " + "\nAnswer: ");
    			counter++;
    			number5 = 20 - (counter*5);
     
    			ans5 = keyboard.nextLine();		
    			}
    	System.out.println("You scored " + number5 + " points out of 20 for that question.\n(Answer: infinite");
     
    //TOTAL SCORING
     
           total = number1 + number2 + number3 + number4 + number5;
    		System.out.println("\n\nYour total score:" + total);
     
       }
    }

    ERROR:
    FiveQuestionsII.java:98: incompatible types
    found   : int
    required: java.lang.String
           total = number1 + number2 + number3 + number4 + number5;
                                                         ^
    1 error

    I'm a beginner, sorry if my code is sloppy.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Making A Quiz With Loops, Incompatible Type Error

    String total;
    In this line of code, you define total as a String.

    total = number1 + number2 + number3 + number4 + number5;
    In this line of code, you attempt to set the value of a String to the value of an int.

    Was this a mistake, or do you want the total of the numbers to be stored in a String?

    Also a quick suggestion:

    !ans5.equals("Infinite") && !ans5.equals("infinite")
    Here I see that you're accounting for different capitalization. You can do this in one method call. Instead of the .equals() method, you can use the .equalsIgnoreCase() method. That way, even if I were to type "InfINiTE", the program would still notice that the answer is correct.
    Last edited by snowguy13; February 16th, 2012 at 06:35 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    diagnonsense (February 16th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Making A Quiz With Loops, Incompatible Type Error

    Thank you so much !

  5. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Making A Quiz With Loops, Incompatible Type Error

    You're welcome! Good luck with your program!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. [SOLVED] Referencing to Method. Incompatible Types Error.
    By mwebb in forum Object Oriented Programming
    Replies: 12
    Last Post: February 5th, 2012, 02:50 PM
  2. i'm making a logical error here but i'm not seeing it.
    By qrts in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 04:03 AM
  3. While Loops - Error
    By ZombieGurL in forum Loops & Control Statements
    Replies: 5
    Last Post: November 9th, 2011, 12:01 AM
  4. type conversion error
    By Xeenix in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 07:29 PM
  5. [SOLVED] Help making an error message.
    By Lost_Secret in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 1st, 2011, 03:48 PM