Hello - how do case 4 to loop back into my main method loop

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 417 Views - Last Post: 11 March 2019 - 02:25 PM Rate Topic: -----

#1 Hip-know-Toad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 19

Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 01:29 PM

this is just a snip of part of the code I am working with but for the life of me I cannot get case 4 to loop back into my main method to let the user resubmit distance in meters to go through the prompts again


I am getting pretty lost on how to bring my selection which is a number back into my main method which is a string arg
Is what I am trying to do achievable?
Should I use an If statement or while to throw the input in case statment 4?



package Hip-know-Toad;

import java.util.Scanner;

public class Hip-know-Toad {


public static void main(String[] args) {

// Variable declarations
int number; //user choice input
double meters; // user distance input

// Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);

// Ask user for distance value in meters
System.out.println(" welcome to the main menu ");
System.out.println("=============================");
System.out.print("Enter a distance in meters: ");
meters = keyboard.nextDouble();

// Check for input greater than zero
while (meters <= 0) {

String error = "Please enter a num greater than zero";
System.out.println(error);
meters = keyboard.nextDouble();
}

// Menu options
System.out.println("Enter a value of 1 - 2 from the menu options: ");
System.out.println("=============================");
System.out.println("1. Conversion control, 2. exit ");
number = keyboard.nextInt();

// number user selected goes through loop
while (number <= 0 || number > 2) {
String invalid = "Invalid - Please use values 1 through 4";
System.out.println(invalid); // number < 1 or number > 4 invalid

meters = keyboard.nextDouble();
}
switch (number) { //number user chooses dictates case statement
case 1:
conversionControl(meters, number); //calls conversion menu method
// sends user to conversion menu
break; // breaks loop - only 1 response needed
case 2:
System.out.print("good bye - exting program ");
System.exit(0);

}
}

// insert conversion menu here
public static void conversionControl(double meters, int number) { // problem here possibly

Scanner keyboard = new Scanner(System.in);

while (meters <= 0) {

String error = " Please enter a num greater than zero ";
System.out.println(error);
meters = keyboard.nextDouble();
}

// Menu options
System.out.println("welcome to conversion control");
System.out.println("=============================");
System.out.println("Enter a value of 1 - 4 from the menu options: ");
System.out.println("1. Convert to kilometers, 2. Convert to inches "
+ " 3. Convert to feet, 4. back to menu ");
number = keyboard.nextInt();

// number user selected goes through loop
while (number <= 0 || number >= 5) {
String invalid = " Invalid - Please use values 1 through 4 ";
System.out.println(invalid); // number < 1 or number > 4 invalid

meters = keyboard.nextDouble();
}
switch (number) { //number user chooses dictates case statement
case 1:
showKilometers(meters); //calls showKilometers method
// calculates and displays kilometers
break; // breaks loop - only 1 response needed
case 2:

showInches(meters); // calls showInches method
// calculates and displays inches
break; // breaks loop - only 1 response needed

case 3:

showFeet(meters); // calls showFeet method
// calculates and displays feet
break; // breaks loop - only 1 response needed

case 4:

System.out.println("good bye - sending to main menu ");
return main(number); --------- problem here

Is This A Good Question/Topic? 0
  • +

Replies To: Hello - how do case 4 to loop back into my main method loop

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,708
  • Joined: 25-December 13

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 01:35 PM

Please wrap the posted code in code tags to get formatting. Unformatted code is hard to read and understand.
Select the code and press the [CODE] button.

This post has been edited by NormR: 10 March 2019 - 01:35 PM

Was This Post Helpful? 1
  • +
  • -

#3 Hip-know-Toad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 19

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 01:40 PM

this is just a snip of part of the code I am working with but for the life of me I cannot get case 4 to loop back into my main method to let the user resubmit distance in meters to go through the prompts again


I am getting pretty lost on how to bring my selection which is a number back into my main method which is a string arg
Is what I am trying to do achievable?
Should I use an If statement or while to throw the input in case statment 4?



[code]package Hip-know-Toad;

import java.util.Scanner;

public class Hip-know-Toad {

public static void main(String[] args) {

// Variable declarations
int number; //user choice input
double meters; // user distance input

// Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);

// Ask user for distance value in meters
System.out.println(" welcome to the main menu ");
System.out.println("=============================");
System.out.print("Enter a distance in meters: ");
meters = keyboard.nextDouble();

// Check for input greater than zero
while (meters <= 0) {

String error = "Please enter a num greater than zero";
System.out.println(error);
meters = keyboard.nextDouble();
}

// Menu options
System.out.println("Enter a value of 1 - 2 from the menu options: ");
System.out.println("=============================");
System.out.println("1. Conversion control, 2. exit ");
number = keyboard.nextInt();

// number user selected goes through loop
while (number <= 0 || number > 2) {
String invalid = "Invalid - Please use values 1 through 4";
System.out.println(invalid); // number < 1 or number > 4 invalid

meters = keyboard.nextDouble();
}
switch (number) { //number user chooses dictates case statement
case 1:
conversionControl(meters, number); //calls conversion menu method
// sends user to conversion menu
break; // breaks loop - only 1 response needed
case 2:
System.out.print("good bye - exting program ");
System.exit(0);

}
}

// insert conversion menu here
public static void conversionControl(double meters, int number) { // problem here possibly

Scanner keyboard = new Scanner(System.in);

while (meters <= 0) {

String error = " Please enter a num greater than zero ";
System.out.println(error);
meters = keyboard.nextDouble();
}

// Menu options
System.out.println("welcome to conversion control");
System.out.println("=============================");
System.out.println("Enter a value of 1 - 4 from the menu options: ");
System.out.println("1. Convert to kilometers, 2. Convert to inches "
+ " 3. Convert to feet, 4. back to menu ");
number = keyboard.nextInt();

// number user selected goes through loop
while (number <= 0 || number >= 5) {
String invalid = " Invalid - Please use values 1 through 4 ";
System.out.println(invalid); // number < 1 or number > 4 invalid

meters = keyboard.nextDouble();
}
switch (number) { //number user chooses dictates case statement
case 1:
showKilometers(meters); //calls showKilometers method
// calculates and displays kilometers
break; // breaks loop - only 1 response needed
case 2:

showInches(meters); // calls showInches method
// calculates and displays inches
break; // breaks loop - only 1 response needed

case 3:

showFeet(meters); // calls showFeet method
// calculates and displays feet
break; // breaks loop - only 1 response needed

case 4:

System.out.println("good bye - sending to main menu ");
return main(number); --------- problem here 
[/code]

View PostNormR, on 10 March 2019 - 01:35 PM, said:

Please wrap the posted code in code tags to get formatting. Unformatted code is hard to read and understand.
Select the code and press the [CODE] button.



done. apologies
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,708
  • Joined: 25-December 13

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 01:44 PM

Can you fix the code's indentations. All the statements should not start in the first column.
Logically nested statements should be indented.
Was This Post Helpful? 1
  • +
  • -

#5 Hip-know-Toad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 19

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 01:47 PM

package wallyjavaassignment5;

import java.util.Scanner;

public class WallyJavaAssignment5 {

    public static void main(String[] args) {

            // Variable declarations
        int number;     //user choice input
        double meters;  // user distance input

        // Scanner object for keyboard input
        Scanner keyboard = new Scanner(System.in);

        // Ask user for distance value in meters
        System.out.println(" welcome to the main menu ");
        System.out.println("=============================");
        System.out.print("Enter a distance in meters: ");
        meters = keyboard.nextDouble();

        // Check for input greater than zero
        while (meters <= 0) {

            String error = "Please enter a num greater than zero";
            System.out.println(error);
            meters = keyboard.nextDouble();
        }

            // Menu options
        System.out.println("Enter a value of 1 - 2 from the menu options: ");
        System.out.println("=============================");
        System.out.println("1. Conversion control, 2. exit ");
        number = keyboard.nextInt();

        // number user selected goes through loop
        while (number <= 0 || number >= 2) {
            String invalid = "Invalid - Please use values 1 through 4";
            System.out.println(invalid); // number < 1 or number > 4 invalid

            meters = keyboard.nextDouble();
        }
        switch (number) {   //number user chooses dictates case statement
            case 1:
                conversionControl(meters, number); //calls conversion menu method
                // sends user to conversion menu
                break; // breaks loop - only 1 response needed
            case 2:
                System.out.print("good bye - exting program ");
                System.exit(0);

        }
    }

 // insert conversion menu here
    public static void conversionControl(double meters, int number) { // problem here possibly

        Scanner keyboard = new Scanner(System.in);

        while (meters <= 0) {

            String error = " Please enter a num greater than zero ";
            System.out.println(error);
            meters = keyboard.nextDouble();
        }

        // Menu options
        System.out.println("welcome to conversion control");
        System.out.println("=============================");
        System.out.println("Enter a value of 1 - 4 from the menu options: ");
        System.out.println("1. Convert to kilometers, 2. Convert to inches "
                + " 3. Convert to feet, 4. back to menu ");
        number = keyboard.nextInt();

        // number user selected goes through loop
        while (number <= 0 || number >= 5) {
            String invalid = " Invalid - Please use values 1 through 4 ";
            System.out.println(invalid); // number < 1 or number > 4 invalid

            meters = keyboard.nextDouble();
        }
        switch (number) {   //number user chooses dictates case statement
            case 1:
                showKilometers(meters); //calls showKilometers method
                // calculates and displays kilometers
                break; // breaks loop - only 1 response needed
            case 2:

                showInches(meters); // calls showInches method
                // calculates and displays inches
                break;       // breaks loop - only 1 response needed

            case 3:

                showFeet(meters); // calls showFeet method
                // calculates and displays feet
                break;   // breaks loop - only 1 response needed

            case 4:

                System.out.println("good bye - sending to main menu ");
                return main(number);

        }
    }

// stop
    public static void showKilometers(double meters) {
        double kilometers;
        kilometers = (meters * .001); // performs calculations
        System.out.print(meters + " meters equals to " + kilometers + " kilometers "); // displays kilometers result
    }

    public static void showInches(double meters) {
        double inches;
        inches = (meters * 39.37); // performs calculations
        System.out.print(meters + " meters equals to " + inches + " inches "); // displays kilometers result
    }

    public static void showFeet(double meters) {
        double feet;
        feet = (meters * 3.281); // performs calculations
        System.out.print(meters + " meters equals to " + feet + " feet "); // displays kilometers result

    }
}

Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,708
  • Joined: 25-December 13

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 01:52 PM

   return main(number);



What is that statement supposed to do? The syntax says to call the method named main, pass it number and return the value that is returned.
However:
main is void (does not return a value)
conversionControl is also void
main takes a String array as an argument, not an int

This post has been edited by NormR: 10 March 2019 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 Hip-know-Toad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 19

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 02:01 PM

so the theory I want is the case 4 statement to route the user back into the main method and go through the prompts again

Main method
Loop function will give the user an option of 1 or 2
1: will take the user back into the conversion method
2: will quit the program

Conversion control method
1: converts meters to kilo
2: converts meters to inches
3: converts meters to feet

4: problem

1: sends user back to the main method loop
2: exits the program
the return statement at the bottom is definately wrong and was my latest attempt of toying with the code to try different things
I am not sure how to send the user back to the main method

line items 099 - 102 needs to send the user back to line item 30 in the main method to have the ability to rerun the program

so the theory I want is the case 4 statement to route the user back into the main method and go through the prompts again

Main method
Loop function will give the user an option of 1 or 2
1: will take the user back into the conversion method
2: will quit the program

Conversion control method
1: converts meters to kilo
2: converts meters to inches
3: converts meters to feet

4: problem

1: sends user back to the main method loop
2: exits the program
the return statement at the bottom is definitely wrong and was my latest attempt of toying with the code to try different things
I am not sure how to send the user back to the main method

line items 099 - 102 needs to send the user back to line item 30 in the main method to have the ability to rerun the program
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,708
  • Joined: 25-December 13

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 02:04 PM

Quote

I want is the case 4 statement to route the user back into the main method

When the method exits, execution will return to the main method to the statement following where the method was called. At that point the code needs to decide where it wants execution to go next.

Quote

to send the user back to line item 30

Put the code inside of a while loop.

This post has been edited by NormR: 10 March 2019 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#9 Hip-know-Toad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 19

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 02:07 PM

so if I take the return out of case 4 and have the main method call the conversion control method I can specifically call on case 4 within the switch statement within the conversion control method?
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,708
  • Joined: 25-December 13

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 02:15 PM

Quote

specifically call on case 4 within the switch statement within the conversion control method

Sorry, I don't understand how that is different from the current code.
If the return statement was corrected to only be:
   return;

The code would compile.

To get the execution to loop back to the code at line 30, add a while loop that starts just before line 30 and includes all the code what needs to be able to loop back.
Was This Post Helpful? 0
  • +
  • -

#11 Hip-know-Toad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 19

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 02:23 PM

I apologize I am not following bare with me a bit

but what would I define If I put a loop before line item 30?

I have while loops before and after for meters and number. If I did add the loop would I just assign a value to case for and set it equal to that and if the expression is true it goes right back down through the program?
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,708
  • Joined: 25-December 13

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 02:40 PM

If you want the code after line 30 to be able to loop back to line 30, wrap that code inside of a while loop:
   while(notDone) {
      // line 30
      // some lines of code
      //  more code that wants to be able to loop back to line 30
   } // end of while

Any execution flow that gets to the end of the while will loop back to line 30.
The execution flow can exit the loop with a break statement or by setting notDone = false;
Was This Post Helpful? 0
  • +
  • -

#13 Hip-know-Toad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 19

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 03:27 PM

I found a solution by duplicating the main method
after that I just called the userMenu method in case 4 and it worked

I really appreciate the help and your patience I am clearly very new to Java and its unexpectedly a fast learning pace and I really do not grasp some of the concepts I should. Once again thank you for your advise

is there a way to edit my posts / replies to clean some of my responses up?


public static void main(String[] args) {

        // Variable declarations
        int number;     //user choice input
        double meters;  // user distance input

        // Scanner object for keyboard input
        Scanner keyboard = new Scanner(System.in);

        // Ask user for distance value in meters
        System.out.println(" welcome to the main menu ");
        System.out.println("=============================");
        System.out.println("Enter a distance in meters: ");

        meters = keyboard.nextDouble();

        // Check for input greater than zero
        while (meters <= 0) {

            String error = "Please enter a num greater than zero";
            System.out.println(error);
            meters = keyboard.nextDouble();
        }

        // Menu options
        System.out.println("Enter a value of 1 - 2 from the menu options: ");

        System.out.println("1. Conversion control, 2. exit ");

        number = keyboard.nextInt();

        // number user selected goes through loop
        while (number <= 0 || number > 2) {
            String invalid = "Invalid - Please use values 1 through 2";
            System.out.println(invalid); // number < 1 or number > 2 invalid

            meters = keyboard.nextDouble();
        }
        switch (number) {   //number user chooses dictates case statement
            case 1:
                conversionControl(meters, number); //calls conversion menu method
                // sends user to conversion menu
                break; // breaks loop - only 1 response needed
            case 2:
                System.out.print("good bye - exting program ");
                System.exit(0); // exits program

        }
    }

    public static void userMenu(double meters, int number) {

        // Scanner object for keyboard input
        Scanner keyboard = new Scanner(System.in);

        // Ask user for distance value in meters
        System.out.println(" "); // place holder for neat display
        System.out.println("=============================");
        System.out.println("Enter a distance in meters: ");

        meters = keyboard.nextDouble();

        // Check for input greater than zero
        while (meters <= 0) {

            String error = "Please enter a num greater than zero";
            System.out.println(error);
            meters = keyboard.nextDouble();
        }

        // Menu options
        System.out.println("Enter a value of 1 - 2 from the menu options: ");

        System.out.println("1. Conversion control, 2. exit ");

        number = keyboard.nextInt();

        // number user selected goes through loop
        while (number <= 0 || number > 2) {
            String invalid = "Invalid - Please use values 1 through 2";
            System.out.println(invalid); // number < 1 or number > 2 invalid

            meters = keyboard.nextDouble();
        }
        switch (number) {   //number user chooses dictates case statement
            case 1:
                conversionControl(meters, number); //calls conversion menu method
                // sends user to conversion menu
                break; // breaks loop - only 1 response needed
            case 2:
                System.out.print("good bye - exting program ");
                System.exit(0);

        }
    }

        // insert conversion menu here
    public static void conversionControl(double meters, int number) { // problem here possibly

        Scanner keyboard = new Scanner(System.in);

        while (meters <= 0) {

            String error = " Please enter a num greater than zero ";
            System.out.println(error);
            meters = keyboard.nextDouble();
        }

        // Menu options
        System.out.println("welcome to conversion control");
        System.out.println("=============================");
        System.out.println("Enter a value of 1 - 4 from the menu options: ");

        System.out.println("1. Convert to kilometers, 2. Convert to inches "
                + " 3. Convert to feet, 4. back to menu ");

        number = keyboard.nextInt();

        // number user selected goes through loop
        while (number <= 0 || number >= 5) {
            String invalid = " Invalid - Please use values 1 through 4 ";
            System.out.println(invalid); // number < 1 or number > 4 invalid

            meters = keyboard.nextDouble();
        }
        switch (number) {   //number user chooses dictates case statement
            case 1:
                showKilometers(meters); //calls showKilometers method
                userMenu(meters, number); // routes user back to usermenu
                // calculates and displays kilometers
                break;        // breaks loop - only 1 response needed

            case 2:

                showInches(meters); // calls showInches method
                userMenu(meters, number); // routes user back to usermenu
                // calculates and displays inches
                break;       // breaks loop - only 1 response needed

            case 3:

                showFeet(meters); // calls showFeet method
                userMenu(meters, number); // routes user back to usermenu
                // calculates and displays feet
                break;      // breaks loop - only 1 response needed

            case 4:

                System.out.println("good bye - sending to main menu ");
                userMenu(meters, number); // routes user back to user menu

Was This Post Helpful? 0
  • +
  • -

#14 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,708
  • Joined: 25-December 13

Re: Hello - how do case 4 to loop back into my main method loop

Posted 10 March 2019 - 03:39 PM

Quote

a solution by duplicating

Duplicating code is not a good solution. It makes code harder to maintain.
It makes the code bigger than it needs to be and can be confusing.

This post has been edited by NormR: 10 March 2019 - 03:42 PM

Was This Post Helpful? 1
  • +
  • -

#15 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6736
  • View blog
  • Posts: 27,734
  • Joined: 12-December 12

Re: Hello - how do case 4 to loop back into my main method loop

Posted 11 March 2019 - 01:50 AM

Note that you do not need, and should not have, a new scanner for each method. Create it once in main.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2