(JAVA )How can I only make my text/table output for a specific input of an integer in my program?

(JAVA )How can I only make my text/table output for a specific input of an integer in my program?
0

#1

When the user inputs an integer including and greater than 6, I want to make the table/question below appear. However, I managed to get this working but I dont want the table/question to affect the integers including/below 5.

As you can see in the test example below when the user inputs an integer from 1-5 when asked System.out.println(username + ", please enter the number next to the task you wish to proceed with." ); the question with the table will display which is not want I want.

How can I make the question/table re appear and go back to that step only if the user inputs a number including or greater than 6?

CODE

import java.util.Scanner;
public class assignment {
    public static void main (String []args){ 
        //assigning text to ` variable
        Scanner input = new Scanner(System.in);
        String username;
        String coursename;
        String option1 = "BTEC 90 Credit Diploma Grade"; //assigning the course name text to variable
        String option2 = "BTEC Extended Diploma Grade"; //assigning the course name text to variable
        String option3 = "Functional Skills"; //assigning the course name text to variable
        String option4 = "Help"; //assigning text to variable
        String option5 = "Exit"; //assigning text to variable
        String outline = "+-----------------------------------------------+";

        //BTEC 90 Credit Diploma Grade
        int DSTAR_DSTAR; //integer required for double distinction star - assigning integer to variable
        int DSTAR_D;
        int DD;
        int DM;
        int MM;
        int MP;
        int PP;

        //BTEC Extended Diploma Grade
        int DSTAR_DSTAR_DSTAR; //integer required for merit - assigning integer to variable
        int DSTAR_DSTAR_D; //integer required for merit - assigning integer to variable
        int DSTAR_D_D;
        int DDD; //integer required for merit - assigning integer to variable
        int DDM; //integer required for merit - assigning integer to variable
        int DMM; //integer required for merit - assigning integer to variable
        int MMM; //integer required for merit - assigning integer to variable
        int MMP; //integer required for merit - assigning integer to variable
        int MPP; //integer required for merit - assigning integer to variable
        int PPP; //integer required for merit - assigning integer to variabl

        //Functional Skills
        String Functional_Pass = "pass"; //integer required for pass - assigning text to variable
        String Functional_Fail = "fail"; //integer required for fail - assigning text to variable
        //variables assigning finished

        // Allows a person to enter his/her name   
        Scanner one = new Scanner(System.in);
        System.out.println("Enter Name:" );  
        username = one.next();
        System.out.println("Welcome to Liam's JAVA program " + username + ". This program allows you to convert your grade into USCAS points.");

        while (true) {
            Scanner two = new Scanner(System.in);
            System.out.println(username + ", please enter the number next to the task you wish to proceed with." );  // Allows a person to enter their age  
            System.out.println(outline);
            System.out.println("| |1|         "+option1+ "      |");  
            System.out.println("| |2|         "+option2+ "       |");  
            System.out.println("| |3|         "+option3+ "                 |"); 
            System.out.println("| |4|         "+option4+ "                              |");   
            System.out.println("| |5|         "+option5+ "                              |");  
            System.out.println(outline);

            int num = input.nextInt();

            if (num == 1) {
                System.out.println("You have chosen the course " +option1+ " to calculate your grade into UCAS points");
            } else if (num == 2) {
                System.out.println("You have chosen the course " +option2+ " to calculate your grade into UCAS points");
            } else if (num == 3) {
                System.out.println("You have chosen the course " +option3+ " to calculate your grade into UCAS points");
            } else if (num == 4) {
                System.out.println("You have chosen the option " +option4+ " the purpose of the program is to calculate your subject grade into UCAS points.");
            } else if (num == 5) {
                System.out.println("You have chosen the option to " +option5+ " the grade calculator.");
                System.out.println("Thanks for using the program " +username+ ".");
                break;
            }  else {
                //System.out.println("Thanks for using the program" +username+ " remember to enter the correct value next time.");
                System.out.println("Please enter the correct number. Please choose a number from the below mentiond table");
                continue;
            }

        }
    }
}

When program is run

Enter Name:
Florian
Welcome to Liam's JAVA program Florian. This program allows you to convert your grade into USCAS points.
Florian, please enter the number next to the task you wish to proceed with.
+-----------------------------------------------+
| |1|         BTEC 90 Credit Diploma Grade      |
| |2|         BTEC Extended Diploma Grade       |
| |3|         Functional Skills                 |
| |4|         Help                              |
| |5|         Exit                              |
+-----------------------------------------------+
1
You have chosen the course BTEC 90 Credit Diploma Grade to calculate your grade into UCAS points
Florian, please enter the number next to the task you wish to proceed with.
+-----------------------------------------------+
| |1|         BTEC 90 Credit Diploma Grade      |
| |2|         BTEC Extended Diploma Grade       |
| |3|         Functional Skills                 |
| |4|         Help                              |
| |5|         Exit                              |
+-----------------------------------------------+

#2

I don’t program in JAVA, but it would seem the following line is going to cause you to break out of the while loop if the number is greater to 5. Maybe you should only break if the options are 1, 2, 3, 4, or 5? Then your else block would only have a break; statement in it.

            }  else {
                System.out.println("Thanks for using the program" +username+ " remember to enter the correct value next time.");
                break;

#3

One thing you could try is to include an if statement with a continue:

if (num >= 6) {
            	System.out.println("Thanks for using the program" +username+ " remember to enter the correct value next time.");
            		continue;
            }

And then you can put breaks after each of your other if/ if else statements.


#4

Hi @ldocherty1,

You just need to change the Break statement with Continue in your else part.

Your problem is to show the text/table when user would enter 6 or greater value.

Here is your solution. Look at the else section

        if (num == 1) {
            System.out.println("You have chosen the course " +option1+ " to calculate your grade into UCAS points");
        } else if (num == 2) {
            System.out.println("You have chosen the course " +option2+ " to calculate your grade into UCAS points");
        } else if (num == 3) {
            System.out.println("You have chosen the course " +option3+ " to calculate your grade into UCAS points");
        } else if (num == 4) {
            System.out.println("You have chosen the option " +option4+ " the purpose of the program is to calculate your subject grade into UCAS points.");
        } else if (num == 5) {
            System.out.println("You have chosen the option to " +option5+ " the grade calculator.");
            System.out.println("Thanks for using the program " +username+ ".");
            break;
        }  else {
            //System.out.println("Thanks for using the program" +username+ " remember to enter the correct value next time.");
            System.out.println("Please enter the correct number. Please choose a number from the below mentiond table");
            continue;
        }

Now if the user would enter 6 or greater than 6, then the text/table will show again.

Hope this would be helpful. :slightly_smiling_face:

Regards
Muhammad


#6

Hi,
For that you need to add a condition which will run only once. Please check I have added a boolean variable.

        boolean bool = true;
        while (true) {
            Scanner two = new Scanner(System.in);
            System.out.println(username + ", please enter the number next to the task you wish to proceed with." );  // Allows a person to enter their age  
            if (bool) {
                System.out.println(outline);
                System.out.println("| |1|         "+option1+ "      |");  
                System.out.println("| |2|         "+option2+ "       |");  
                System.out.println("| |3|         "+option3+ "                 |"); 
                System.out.println("| |4|         "+option4+ "                              |");   
                System.out.println("| |5|         "+option5+ "                              |");  
                System.out.println(outline);
                bool = false;
            }
            int num = input.nextInt();

Suggestion: You have sandwiched everything in main() which is not a good practice. Only use main() to write the code which is responsible to run the program. Distribute your code properly in methods which will look proper and easy to read and understand. Variable declaration should go in declaration section.

Hope this will be helpful for you and keep coding :slight_smile:

_s.Muhammad Tariq