Jump to content

Need help with Java

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
public void mainmenu ()

    {

        title ();

        System.out.print("If you wish to exit press 5, if you want to go the intro press 2, and if you want to continue 3:");


        do

        {

            try

            {

                procstr = c.readLine ();

                proc = Integer.parseInt (procstr);

                if (proc == 3)

                    askData ();

                if (proc == 2)

                    intro ();

                if (proc == 5)

                    goodbye ();

                break;

            }

            catch (NumberFormatException e)

            {

              System.out.print ("That's not an integer. Please Try Again");

            }

        }

        while (proc != 5 || proc != 2 || proc != 3);

    }

It doesn't loop if I input a number other than 5,2 or 3. Help? Thanks in advance.

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Try using a boolean variable to determine whether you should continue looping or not.

For instance, create your boolean variable, set it to true.
Now once the user decides to exit the program, under your goodbye() method you can set your variable to false.

Example:

bool continueLooping? = true;


do { 

  doWork();

} while( continueLooping? );


...

private goodBye() {

   continueLooping? = false;

}



#3
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
But then how does it determine that if it's not 5,3, or 2 it should keep on looping if I use a boolean?

#4
Directive

Directive

    Newbie

  • Members
  • Pip
  • 5 posts
The boolean will act as a flag to determine when to stop looping. It's set to true by default and updated only if the user inputs the exit value, which in this case is 5. If the user inputs a 5, the flag is set to false thus terminating the loop. Here's the basic idea:



//The flag

boolean runLoop = true;

//The loop runs as long as runLoop is true

while (runLoop)

{

   //check the input and do the appropriate action.

   if (userInput == 2)

     ...

   if (userInput == 3)

     ...

   if (userInput == 5)

     runLoop = false

}

Hope this helps!

#5
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
if (proc == 5 || proc ==3 || proc == 2)

                    runLoop = false;
That's what I did but it still doesn't work.

---------- Post added at 07:15 PM ---------- Previous post was at 07:10 PM ----------

private double convertLiquid (double none2 ,char unit2, char amount2)

    {

        if (unit == 1)

            return (amount * 33.8140227);

        if (unit == 2)

            return (amount * 0.001);

        if (unit == 3)

            return (amount * 1000);

        if (unit == 4)

            return (amount * 0.00852167911);

        if (unit == 5)

            return (amount * 4.22675284);

        if (unit == 6)

            return (amount * 202.884136);

        if (unit == 7)

            return (amount * 67.6280454);

        if (unit == 8)

            return (amount * 0.264172052);

        if (unit == 9)

            return (amount * 1.05668821);

        if (unit == 10)

            return (amount * 2.11337642);

        else

            return (none2);

    }



    public void displayData ()

    {

        c = new Console ();

        title ();

        c.println ("Your amount is " + amount + " converted to " + convertLiquid(none2, unit2, amount2));

        c.println ("If you want to go back the main menu press 1, if you wish to exit press 0: ");

        while (true)


            {

                try

                {

                    procstr = c.readLine ();

                    proc = Integer.parseInt (procstr);

                    if (proc == 1)

                        mainmenu ();

                    if (proc == 0)

                        goodbye ();

                    break;

                }

                catch (NumberFormatException e)

                {

                    c.println ("That's not an integer. Please Try Again");

                }

            }



    }

I'm also having problems with the parameters. It keeps on getting an error, that none2 isn't found... I don't know..

#6
Directive

Directive

    Newbie

  • Members
  • Pip
  • 5 posts
if (proc == 5 || proc ==3 || proc == 2)

                    runLoop = false;

What this means is: "if the user input is 5 or 3 or 2 then set the flag to false."

From what I'm getting, you want the loop to keep running unless the user inputs 5. In that case then the flag only needs to be set to false when the input is 5, that way the loop only terminates in that situation.

#7
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
No. I want the loop to keep running until the user either inputs 5,3, or 2.

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
It doesn't loop because you break out of the loop every time.
The code from your first post would've worked without the break.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users