Question

import java.util.Scanner;

public class Assignment6 {

public static void commandList()
{

    System.out.println("Command Options------------");
    System.out.println("a: Create a new table");
    System.out.println("b: Change the row sizes");
    System.out.println("c: Change the column sizes");
    System.out.println("d: Change the data types");
    System.out.println("e: Change the formats");
    System.out.println("f: Display the table");
    System.out.println("g: Display the log data");
    System.out.println("?: Display this menu");
    System.out.println("q: Quit the program");
    System.out.println("---------------------------");
}


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

    System.out.println("Command Options------------");
    System.out.println("a: Create a new table");
    System.out.println("b: Change the row sizes");
    System.out.println("c: Change the column sizes");
    System.out.println("d: Change the data types");
    System.out.println("e: Change the formats");
    System.out.println("f: Display the table");
    System.out.println("g: Display the log data");
    System.out.println("?: Display this menu");
    System.out.println("q: Quit the program");
    System.out.println("---------------------------");

    String input = "";
    System.out.println("Please input a command:");
    input = in.nextLine();


    do
    {
if (input.equals("a"))
{
    System.out.println("a [Input three integers to ititialze a table:] ");
    int newTable = in.nextInt();
}
    else if (input.equals("b"))
{
    System.out.println("Change the row sizes");
    int newRow = in.nextInt();
}
else if (input.equals("c"))
{
    System.out.println("c [Type an integer to update the table column]: ");
    int newColumn = in.nextInt();
}
else if (input.equals("d"))
{
    System.out.println("d [Type an integer to update the data type]: ");
    int newDataType = in.nextInt();
}
else if (input.equals("e"))
{
    System.out.println("e [Type and integer to update printf format]: ");
    int newPrintf = in.nextInt();
}
else if (input.equals("f"))
{
    System.out.println("f [Display the table]");
}
else if (input.equals("g"))
{
    System.out.println("g [Display the log data]");
}
else if (input.equals("?"))
{
    commandList();
}
else
    {
    System.out.println("Invalid ***Type ? to get commands***");
    }
    }
    while (!input.equals("q"));
{

}

}
    }

I created a menu and I am asking the user to input a letter and the program will show a command option they choose. Right now I have it so if the user inputs "a" then "Input three integers to initialize the table" will print. I need it to then print "Please input a command" next but it just keeps printing "Input three integer to itialize the table" I have been trying different methods for a while now and I have no idea what to do. Any help?

Was it helpful?

Solution

Look at the bottom of your do-while loop:

do
{
    if (input.equals("a"))
    {
        System.out.println("a [Input three integers to ititialze a table:] ");
        int newTable = in.nextInt();
    }
    // … more if-else statements

} while (!input.equals("q"));

It keeps printing that statement because after the if-else statement finishes, the value of input still equals "a" since you haven't changed the value of input.

Perhaps you want the user to enter new input before the while loop checks again.

    // … more if-else statements

    input = in.nextLine(); // <-- add new user input 

} while (!input.equals("q"));

And as Juned Ahsan has already said you need to add more code within that first if statement to print the other commands that you mentioned.

OTHER TIPS

You need to loop to ask for three integers. Better use array to capture the values. Somethign like this:

if (input.equals("a"))
{
    System.out.println("a [Input three integers to ititialze a table:] ");
    int newTable[] = new int[3];
    for (int i = 0; i < 3 ; i++) {
        System.out.println("Input " + (i+1) + "table value: ");
        newTable[i] = in.nextInt();
    }

}

Since depending on the command entered by the user, the number of inputs you accept from the user changes, try using loops inside the if or else conditions. A do while loop will just keep going the same number of times for any of the commands entered. Try something like this:

String input = "";

do {
    System.out.println("Please input a command:");
    input = in.nextLine();

    if (input.equals('a')) {  
        System.out.println("a [Input three integers to ititialze a table:] ");
        int newTable[] = new int[3];
        for (int i = 0; i < 3 ; i++) {
            System.out.println("Input " + (i+1) + "table value: ");
            newTable[i] = in.nextInt();
        }
    }
    else if(input.equals('b')) {
        // Accept as many inputs you want for command 'b'
    }
} while (!input.equals('q'))

The outer do while loop will ensure that you can accept as many commands from the user till the user enters 'q'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top