0

Hello there I want to make a quiz app and so far I've done this.

import java.util.Scanner;
public class Brotcrunsher {
  public static void main(String args[]){
    Scanner scan = new Scanner(System.in);

    System.out.println ("Hallo");
    System.out.println ("Welcome to the test");
    System.out.println ("If you are the admin of the test and would like to edit it please type the password now");
    System.out.println ("Otherwhise please say 'begin' to start");
    String c = scan.nextLine();
    if (c.equals ("edit")) {
      System.out.println ("Editor mode coming soon, please edit via code"); 
    } 
    else {
      if (c.equals ("begin")) {  
      } // end of if
      System.out.println ("A flag has more then 1 color right?");
      String a = scan.nextLine();
      if (a.equals("ja")) {
        System.out.println ("You arent dumb, nice.");
        System.out.println ("What is 352 + 223");
        int b = scan.nextInt(); 
        if (b == 575) {
          System.out.println ("That is correct nice");
          System.out.println ("You passed the basic questions, going to be tricky now");
          System.out.println ("What was the date when Donald Trump became president");
          String g = scan.nextLine();
          System.out.println ("A = 19.01 B = 20.01 C = 21.01 D= 22.01");
          if (g.equals ("B")){
            System.out.println ("You are right, lets move on.");
          } // end of if
          else {
            System.out.println ("That is wrong. Better luck next time");
          } // end of if-else  
        }  
        else {
          System.out.println("That isn't right, you failed.");
          System.out.println ("If you'd like to repeat the test, go ahead and do so");
        }  
      }
      else {
        System.out.println ("You arentn a genie");
      } 
    }
  } 
}

So my question is where do I need to place the String g = scan.NextLine to get it working? I dont get any compiling error just the console exit's. I tried placing it at the beggining in the IF statement and on some other positions but it just stoppped. Please help.

Sagar V
  • 11,083
  • 7
  • 41
  • 62
Kaipo DaGod
  • 45
  • 1
  • 1
  • 5

4 Answers4

1

If the console closes when you're running the application then you are probably getting a runtime error. Try running your program from an already open command shell to see the error.

This should be a comment, but I do not have enough rep 0:)

PimJ
  • 38
  • 7
  • Like its not closing but its pausing it... and when I press a key it closes. – Kaipo DaGod Mar 07 '17 at 11:40
  • Then it's probably waiting for your input – PimJ Mar 07 '17 at 11:41
  • When your input has been given (pressed a key), the program might still terminate due to a runtime error. Try running the app from the command line: http://stackoverflow.com/questions/16137713/how-to-run-a-java-program-from-the-command-line – PimJ Mar 07 '17 at 11:42
1

The problem is that the scanner is not empty when you arrive at the critical part of the code. You need to clear the buffer EX:

public class Brotcrunsher {
      public static void main(String args[]){
        Scanner scan = new Scanner(System.in);

        System.out.println ("Hallo");
        System.out.println ("Welcome to the test");
        System.out.println ("If you are the admin of the test and would like to edit it please type the password now");
        System.out.println ("Otherwhise please say 'begin' to start");
        String c = scan.nextLine();
        if (c.equals ("edit")) {
          System.out.println ("Editor mode coming soon, please edit via code"); 
        } 
        else {
          if (c.equals ("begin")) {  
          } // end of if
          System.out.println ("A flag has more then 1 color right?");
          String a = scan.nextLine();
          if (a.equals("ja")) {
            System.out.println ("You arent dumb, nice.");
            System.out.println ("What is 352 + 223");
            int b = scan.nextInt(); 
            if (b == 575) {
              System.out.println ("That is correct nice");
              System.out.println ("You passed the basic questions, going to be tricky now");
              System.out.println ("What was the date when Donald Trump became president");
              System.out.println ("A = 19.01 B = 20.01 C = 21.01 D= 22.01");
              scan.nextLine();     // FEED the new-line character
              String g = scan.nextLine();
              if (g.equals ("B")){
                System.out.println ("You are right, lets move on.");
              } // end of if
              else {
                System.out.println ("That is wrong. Better luck next time");
              } // end of if-else  
            }  
            else {
              System.out.println("That isn't right, you failed.");
              System.out.println ("If you'd like to repeat the test, go ahead and do so");
            }  
          }
          else {
            System.out.println ("You arentn a genie");
          } 
        }
      } 
    }

More details: How can I clear the Scanner buffer in Java?

Community
  • 1
  • 1
betontalpfa
  • 2,402
  • 1
  • 23
  • 44
0

I tried your code and the last scan.nextLine() seems to be skipped. Have a look at this post. The position of your call is not the problem.

Community
  • 1
  • 1
0

I think the code System.out.println ("A = 19.01 B = 20.01 C = 21.01 D= 22.01"); should be in front of String g = scan.nextLine(); because you need to give the options to the user first and then the user will make a choice.

Lee
  • 27
  • 4