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 9 of 9

Thread: It won't display the invalid selection part

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default It won't display the invalid selection part

    Hello I have a problem in my coding. This coding is a Quiz Game creator. Lets say I want to create 3 question in this game.
    and when the game starts.. it will display the 3 question.

    So at that selection of question I chose to do Number 5 when it only has 3. It should have displayed a message saying Invalid selection please try again. But instead the program display a Question 5 but with null data.

    How can I make my program display that message? It doesnt seem to work when I try the IF statement it tends to skip that if statement or I typed something wrong but I don't see where it is.
     
    /**
     * Write a description of class QuizGamePA3 here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.*;
    import java.io.*;
    import java.util.Scanner;
    public class QuizGamePA3
    {
      public static void main(String[]args ) throws FileNotFoundException
    {  
        Scanner input = new Scanner (System.in);
     
         String s = " ";
     
        int[] choice,answer,pick,pick2,numbers;
        choice = new int[5000];
        answer = new int[5000];
        numbers= new int[5000];
        pick = new int[5000];
        pick2= new int[5000];
         int alt = 1;
     
        String[] data,category,option,option2,option3,option4;
        data = new String[5000];
        category = new String[5000];
        option = new String[5000];
        option2= new String[5000];
        option3= new String[5000];
        option4= new String[5000];
     
        int Point=0;
     
        System.out.printf("\n%10s Welcome to Quiz Game Creator \n\n",s);
        System.out.printf("%16s How to do it?%n",s);
        System.out.printf("1] Specify number of question you want to do.\n");
        System.out.printf("2] Create the question CATEGORY\n");
        System.out.printf("3] Enter the QUESTION for each category.\n");
        System.out.printf("4] Enter the OPTION for each question.\n");
        System.out.printf("5] Provide the ANSWER for each question.\n");
        System.out.printf("6] Then your QUIZ GAME is ready to be play by your friends\n");
        System.out.println();
        System.out.println("-----------------------------------------------");
        System.out.println("Press the ENTER key to start creating the Quiz"); 
        System.out.println("-----------------------------------------------");
        input.nextLine();
     
        System.out.printf("\nHow many questions you want to create: ");
        choice[1] = input.nextInt(); // Input of how many questions to create
        System.out.println();
     
        System.out.print('\u000C'); 
        input.nextLine();
        System.out.println();
        System.out.println(" Please enter your question CATEGORY");
     
        for(int a=1; a<=choice[1];a++){
     
           System.out.print(numbers[1]+1+ "] ");
           data[a]=input.nextLine();
           numbers[1]++;       
     
          }
        System.out.print('\u000C'); 
        System.out.println();
        System.out.println(" Please enter your QUESTION");
     
        for(int b=1; b<=choice[1];b++){
           System.out.println();    
           System.out.println("Category: " +data[b]);
           System.out.printf(choice[3]+1+"] ");
           category[b] = input.nextLine();// Input of the questions for certain category
           choice[3]=choice[3]+1;
            }
        System.out.print('\u000C'); 
        System.out.println(); 
        System.out.println("Please enter your question OPTION");
     
        for(int c = 1; c<=choice[1];c++){
     
          System.out.println();
          System.out.println("Question : "+category[c]);// Storing of 4 options for each questions
          System.out.println();
     
          System.out.printf("1] ");
          option[c] = input.nextLine();
     
          System.out.printf("2] ");
          option2[c] = input.nextLine();
     
          System.out.printf("3] ");
          option3[c] = input.nextLine();
     
          System.out.printf("4] ");
          option4[c] = input.nextLine();  
     
          System.out.println();
          System.out.println("Please Set the answer to this question");
          System.out.print("The answer is: ");
          answer[c] = input.nextInt();// Chosen answer for each questions.
          input.nextLine();
          System.out.println();
         }
         System.out.println("Your Quiz Game is now READY. Press the ENTER key to start...");
         input.nextLine();
         System.out.print('\u000C'); 
     
         System.out.printf("%15s Welcome to the QUIZ GAME\n\n",s);
         System.out.printf("%8s Game Rule: 1] Select one topic.\n",s);
         System.out.printf("%18s 2] Answer the MCQ question.\n",s);
         System.out.printf("%18s 3] Each correct answer will be awarded 10 points.\n",s);
         System.out.printf("%18s 4] Each incorrect answer will be deducted 10 points.\n",s);
         System.out.printf("%18s 5] The game will be over once you answer the question wrongly.\n",s);
         System.out.println();
         System.out.printf("%10s Press the ENTER key to start the game...\n",s);
         input.nextLine();
         System.out.print('\u000C'); 
     
     
         for(int z=1; z<=choice[1];z++){
     
          System.out.println(" Your current point is: "+Point); // game currency
         for(int d=1; d<=choice[1];d++){
     
            System.out.println((d)+ "] "+data[d]);// display the categories of the questions
     
            }
           System.out.print("Please select question topic : ");
           pick[z]=input.nextInt();// This is where the user choose which category to answer
     
     
            if (pick2[z] > choice[1]) {
            System.out.println();
            System.out.println("Invalid number. Please re-select your selection");
            System.out.println();
     
             } 
          else if(pick2[z] <= choice[1]) {
              System.out.println("--------------------------------------");
                System.out.println();
            System.out.println("Category: "+data[pick[z]]);
            System.out.println("Question: "+ category[pick[z]]);
            System.out.println();
            System.out.println("1] "+option[pick[z]]);
            System.out.println("2] "+option2[pick[z]]);
            System.out.println("3] "+option3[pick[z]]);
            System.out.println("4] "+option4[pick[z]]);
            System.out.print("What is your answer: ");
            pick2[z]=input.nextInt();
              if (pick2[z] == answer[pick[z]]){
             Point=Point + 10;
             System.out.println();
             System.out.println("Your answer is correct! You earn 10 Points");
             System.out.println();
             System.out.println("------------------------------------------");
     
            }
     
            else{ 
              System.out.print('\u000C'); 
              Point=Point-10;
              System.out.println();
              System.out.println("Your answer is WRONG!");
              System.out.println("      GAME OVER!");
              System.out.println();
              System.out.println("Your final point is :"+Point+" Point");
              System.out.println();
              System.out.println(" Thanks your for playing");
              input.nextLine();
              System.exit(0);
            }
          } 
     
        }
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,608
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: It won't display the invalid selection part

    There may be a problem using the Scannet class's methods. See this: Java Scanner not working, difference between next() and nextLine() | Rommel Rico
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: It won't display the invalid selection part

    Could you give some sample input/output so we can see how you got to the point of the error in your code?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: It won't display the invalid selection part

    Quote Originally Posted by Norm View Post
    There may be a problem using the Scannet class's methods. See this: Java Scanner not working, difference between next() and nextLine() | Rommel Rico
    Sorry norm get rid of the System.out.print('\u000C');
    Im using BlueJ
    Quote Originally Posted by Chris.Brown.SPE View Post
    Could you give some sample input/output so we can see how you got to the point of the error in your code?
    here..
    output.jpg

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: It won't display the invalid selection part

    The line i am looking at is as follows.

            pick[z]=input.nextInt();// This is where the user choose which category to answer
     
     
            if (pick2[z] > choice[1]) {
            System.out.println();
            System.out.println("Invalid number. Please re-select your selection");
            System.out.println();
     
             }

    Your input is going into pick[z] but then you are doing your if statement on pick2[z]. Is this as expected? I dont see anywhere before this line that pick2 is set.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  6. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    Evilreaper (March 29th, 2013)

  7. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: It won't display the invalid selection part

    ah i see that's what i did wrong... no wonder it wont run as i wanted it to be.. thx for help.~ i had 2 pick var... it works fine now. thanks again.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,608
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: It won't display the invalid selection part

    Could you explain how::
    System.out.print('\u000C');
    Fixes the Scanner problem
    and provide a link to where you found that technique?

    Other students can probably use that technique.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: It won't display the invalid selection part

    Quote Originally Posted by Norm View Post
    Could you explain how::
    System.out.print('\u000C');
    Fixes the Scanner problem
    and provide a link to where you found that technique?

    Other students can probably use that technique.
    System.out.print('\u000C');
    umm the code is just part of the coding inside Bluej that allows the user to clearscreen. I read it online here.

    Bluej for ICSE Schools: Clearing the terminal

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,608
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: It won't display the invalid selection part

    This is specific to the BlueJ terminal
    Then it's probably not much use to others.

    Thanks.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why won't this image display?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2012, 09:05 PM
  2. invalid encoding for signature
    By Silvery in forum JDBC & Databases
    Replies: 2
    Last Post: July 2nd, 2012, 03:20 PM
  3. Won't display calculated fahrenheit and celsius when running.
    By smithmar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 17th, 2012, 08:00 PM
  4. String won't display in my window
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 24th, 2011, 03:23 PM
  5. Identifying invalid calls
    By joshft91 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 26th, 2011, 04:38 PM

Tags for this Thread