-1

All menu options work besides menu option 1 when i select 1 it just repeats the menu instead of asking me to type the name and repeat it 20 time, if anyone can help me debug I would appreciate it.

  1. Get the user’s first name and echo it back out 20 times.

  2. Get the Store user’s age and double it and display the age and the doubled age.

  3. Using the age from #2 output one of the following statements.Since you are 99 years old, you are a teenagerb. Since you are 99 years old, you are NOT a teenager

  4. Get a single integer between 3 and 50 from the user. Create a triangle of X’s with the integer inputted rows. The triangle needs to be displayed on the screen and in a text document named triangle.txt.

The code :

//Menu.java

import java.util.Scanner;

public class Menu {

  public static void main(String[] args) {
    /*
     * Creating an Scanner class object which is used to get the inputs
     * entered by the user
     */
    Scanner sc = new Scanner(System.in);
    int choice;
    do {
      System.out.println("\n::MENU::");
      System.out.println("1.Echo Name");
      System.out.println("2.Double Your Age");
      System.out.println("3.Check Teenager or not");
      System.out.println("4.Display Triangle");
      System.out.println("5.Exit");
      System.out.print("Enter Choice:");
      choice = sc.nextInt();
      switch (choice) {
      case 1: {
        System.out.print("Enter your name");
        String name = sc.nextLine();
        for (int i = 1; i <= 20; i++) {
          System.out.println(name);
        }
        break;
      }
      case 2: {
        int age;
        System.out.print("Enter age :");
        age = sc.nextInt();
        System.out.println("Your age :" + age);
        System.out.println("Doubled age :" + (2 * age));

        break;
      }
      case 3: {
        int age;
        System.out.print("Enter age :");
        age = sc.nextInt();

        if (age >= 13 && age <= 19) {
          System.out.println("Since you are " + age + " years old.Your are a Teenager");
        } else {
          System.out.println("Since you are " + age + " years old.Your are not a Teenager");
        }
        break;
      }
      case 4: {
        int rows;
        do {
          System.out.print("Enter a number (between 3 and 50):");
          rows = sc.nextInt();
          if (rows < 3 || rows > 50) {
            System.out.println("** Invalid.Must be between 3 and 50 **");
          }
        } while (rows < 3 || rows > 50);
        printTriangle(rows);
        break;
      }
      case 5: {
        break;
      }
      default: {
        System.out.println("** Invalid Choice **");
        break;
      }

      }
    } while (choice != 5);

  }

  private static void printTriangle(int size) {
    char ch = '*';
    for (int a = 0; a < 2 * size - 1; a++) {
      if (a % 2 == 0) {
        for (int b = 0; b < (2 * size - 1) - a; b++) {
          System.out.print(" ");
        }

        for (int c = 0; c <= a; c++) {
          System.out.print(ch + " ");
        }
        System.out.println();
      }

    }

  }

}
Martheen
  • 5,198
  • 4
  • 32
  • 55
Bri S
  • 23
  • 1
  • 1
  • 6
  • Where is the code that display your main menu? – Martheen Jun 04 '21 at 03:24
  • import java.util.Scanner; public class Menu { public static void main(String[] args) { /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in); int choice; do { System.out.println("\n::MENU::"); System.out.println("1.Echo Name"); System.out.println("2.Double Your Age"); System.out.println("3.Check Teenager or not"); System.out.println("4.Display Triangle"); System.out.println("5.Exit"); System.out.print("Enter Choice:"); choice = sc.nextInt(); switch (choice) { I posted code above – Bri S Jun 04 '21 at 03:25
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Martheen Jun 04 '21 at 03:31
  • Also see https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it for writing to file – Martheen Jun 04 '21 at 03:34
  • @Martheen no its not fixing it – Bri S Jun 04 '21 at 03:35
  • I just tried the solution in that answer and it works. – Martheen Jun 04 '21 at 03:36
  • @Martheen I tried the nextLine but I suppose i'm not placing it correctly because its not working. Like I said i'm only getting the bug with menu option 1 all other menu options run smooth. – Bri S Jun 04 '21 at 03:38
  • Put the nextline right before you're trying to read name https://onlinegdb.com/rGCdzPTWo – Martheen Jun 04 '21 at 03:41
  • Add `sc.nextLine();` after `choice = sc.nextInt();` to clear cache, so that it will wait for your input , or it will read '\n' as `name` value. – meng Jun 04 '21 at 03:56
  • @Martheen thank you it solved the problem how do I put your help as the correct answer? – Bri S Jun 04 '21 at 03:57
  • Since it comes from the dupe, I think you should mark this question as duplicate – Martheen Jun 04 '21 at 03:58
  • @Martheen how do I do that? – Bri S Jun 04 '21 at 03:59

2 Answers2

0

After taking input for choice you have to write sc.nextLine() like

choice = sc.nextInt();

sc.nextLine();

switch (choice) {

because nextInt() only reads in until it's found the int and then stops.

You have to do nextLine() because the input stream still has a newline character and possibly other non-int data on the line. Calling nextLine() reads in whatever data is left, including the enter the user pressed between entering an int and entering a String.

Shubhankar Naik
  • 374
  • 1
  • 3
  • 13
0

To read both strings and integer value, a solution is to use two Scanners:
//Menu.java import java.util.Scanner;

public class Menu {
    public static void main(String[] args) {
        /*
         * Creating an Scanner class object which is used to get the inputs
         * entered by the user
         */
        Scanner intScanner = new Scanner(System.in);
        //added new separate scanner for string 
        Scanner stringScanner = new Scanner(System.in);
        int choice;
        do {

            int age;
            System.out.println("\n::MENU::");
            System.out.println("1.Echo Name");
            System.out.println("2.Double Your Age");
            System.out.println("3.Check Teenager or not");
            System.out.println("4.Display Triangle");
            System.out.println("5.Exit");
            System.out.print("Enter Choice: ");
            choice = intScanner.nextInt();
            switch (choice) {
                case 1: {
                    System.out.print("Enter your name: ");
                    String name = stringScanner.nextLine();
                    for (int i = 1; i <= 20; i++) {
                        System.out.println(name);
                    }
                    break;
                }
                case 2: {
                    System.out.print("Enter age :");
                    age = intScanner.nextInt();
                    System.out.println("Your age :" + age);
                    System.out.println("Doubled age :" + (2 * age));
                    break;
                }
                case 3: {
                    System.out.print("Enter age :");
                    age = intScanner.nextInt();

                    if (age >= 13 && age <= 19) {
                        System.out.println("Since you are " + age + " years old.Your are a Teenager");
                    } else {
                        System.out.println("Since you are " + age + " years old.Your are not a Teenager");
                    }
                    break;
                }
                case 4: {
                    int rows;
                    do {
                        System.out.print("Enter a number (between 3 and 50):");
                        rows = intScanner.nextInt();
                        if (rows < 3 || rows > 50) {
                            System.out.println("** Invalid.Must be between 3 and 50 **");
                        }
                    } while (rows < 3 || rows > 50);
                    printTriangle(rows);
                    break;
                }
                case 5: {
                    System.out.println("You pressed Exit");
                    break;
                }
                default: {
                    System.out.println("** Invalid Choice **");
                    break;
                }

            }
        } while (choice != 5);

    }

    private static void printTriangle(int size) {
        char ch = '*';
        for (int a = 0; a < 2 * size - 1; a++) {
            if (a % 2 == 0) {
                for (int b = 0; b < (2 * size - 1) - a; b++) {
                    System.out.print(" ");
                }

                for (int c = 0; c <= a; c++) {
                    System.out.print(ch + " ");
                }
                System.out.println();
            }

        }

    }

}