0

new to Java as you can tell when you see my question. I want to:

  • Prompt the user with the conversion menu
  • If they select 5, for example, ask them for an input to convert to kilograms
  • Keep giving them the menu until they enter 7 to quit

The problems with my code are - Whatever number is hit initially, step 2 from above doesn't appear. It only appears after the second number is entered -When 7 in entered it doesn't actually quit

I'm also new to this site so my apologies if I'm not asking the question properly

Here's the code

class Test
{
    public static void main(String[] args)
    {
        int number;
        int index;
        int input;
        Double conversion;

        index = 1;

        System.out.println("Enter 1 for Fahrenheit to Celius ");
        System.out.println("Enter 2 for Celius to Fahrenheit ");
        System.out.println("Enter 3 for Inches to Centimetres ");
        System.out.println("Enter 4 for Centimetres to Inches ");
        System.out.println("Enter 5 for Pounds to Kg ");
        System.out.println("Enter 6 for Kg to Pounds ");
        System.out.println("Enter 7 to quit ");
        number = EasyIn.getInt();

        for (index=1; index <=6; index++)

        {
            System.out.println("Enter 1 for Fahrenheit to Celsius ");
            System.out.println("Enter 2 for Celsius to Fahrenheit ");
            System.out.println("Enter 3 for Inches to Centimetres ");
            System.out.println("Enter 4 for Centimetres to Inches ");
            System.out.println("Enter 5 for Pounds to Kg ");
            System.out.println("Enter 6 for Kg to Pounds ");
            System.out.println("Enter 7 to quit ");
            number = EasyIn.getInt();

        if (number == 1)
            {
                System.out.print("Input an amount to convert to Celsius ");
                input = EasyIn.getInt();
                conversion = (5.0/9.0) * (input - 32);
                System.out.println("Your input to Celsius is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 2)
            {
                System.out.print("Input an amount to convert to Fahrenheit ");
                input = EasyIn.getInt();
                conversion = input * 2.8;
                System.out.println("Your input to Fahrenheit is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 3)
            {
                System.out.print("Input an amount to convert to Centimetres ");
                input = EasyIn.getInt();
                conversion = input * 2.54;
                System.out.println("Your input to Centimtres is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 4)
            {
                System.out.print("Input an amount to convert to Inches ");
                input = EasyIn.getInt();
                conversion = input * 0.393701;
                System.out.println("Your input to Inches is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 5)
            {
                System.out.print("Input an amount to convert to Kg ");
                input = EasyIn.getInt();
                conversion = input * 0.453592;
                System.out.println("Your input to Kgs is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 6)
            {
                System.out.print("Input an amount to convert to Pounds ");
                input = EasyIn.getInt();
                conversion = input * 2.20462;
                System.out.println("Your input to pounds is " + conversion);
                System.out.println("Press 7 to quit");
            }
        }
    }
}
8
  • Why do you ask the question outside and inside the loop? what do you want to achieve? Commented Dec 12, 2013 at 20:59
  • Remove the system.out.println and getInt from outside the for loop, and try again. Commented Dec 12, 2013 at 21:00
  • Logan, care to share? Commented Dec 12, 2013 at 21:00
  • Try stepping through it manually, line by line -- write it out on paper. It should be apparent. Commented Dec 12, 2013 at 21:01
  • @Compass that helped one of the problems thank you. Commented Dec 12, 2013 at 21:03

7 Answers 7

3
System.out.print("Input an amount to convert to Celsius ");

This output won't reach your display until the System.out stream is either flushed explicitly or a newline is printed to it. You go on to expect user input before either happens, therefore you don't see the prompt.

This is so because in general, output streams are buffered to optimize performance when much data is sent over them. PrintStream has autoflush behavior, which makes this issue invisible most of the time. It works by flushing at each end-of-line mark, which you don't send with print.

Sign up to request clarification or add additional context in comments.

1 Comment

+1=>Good point I was sure he printed println, i didn't notice that. ops... checked... he used println! Doesn't it flush automatically?
1

You're setting number before the loop and then immediately in the loop. That's why it ignores your first value. Remove

number = EasyIn.getInt();

before the for loop.

Also it doesn't quit because you don't tell it what to do when 7 is entered. You need a new if block for that situation:

if(number == 7) {
    //bla bla
    break;
}

1 Comment

Thank you. I thought for (index=1; index <=6; index++) would make it quit if 7 was entered
0

Couple of things I see. First, you prompt outside your For loop. then you prompt again inside your For loop. That is why it ignores the first number. Get rid of all those System.out.print calls and the GetInt() outside the For loop.

Next, Why are you using a For loop with an index? your code will only loop 6 times then it will end. Every time. No more, no less.

Switch it to a while(true).

To make the quit on seven work.

Add this

if(number == 7)
{
    break;
}

1 Comment

Thanks, that helped. I don't know, I've only been learning loops for a couple of weeks and I thought that was needed.
0
public static void main(String[] args){        
    int number;
    int index;
    int input;
    Double conversion;

    do{

    System.out.println("Enter 1 for Fahrenheit to Celius ");
    System.out.println("Enter 2 for Celius to Fahrenheit ");
    System.out.println("Enter 3 for Inches to Centimetres ");
    System.out.println("Enter 4 for Centimetres to Inches ");
    System.out.println("Enter 5 for Pounds to Kg ");
    System.out.println("Enter 6 for Kg to Pounds ");
    System.out.println("Enter 7 to quit ");
    number = EasyIn.getInt();


    if (number == 1)
        {
            System.out.print("Input an amount to convert to Celsius ");
            input = EasyIn.getInt();
            conversion = (5.0/9.0) * (input - 32);
            System.out.println("Your input to Celsius is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 2)
        {
            System.out.print("Input an amount to convert to Fahrenheit ");
            input = EasyIn.getInt();
            conversion = input * 2.8;
            System.out.println("Your input to Fahrenheit is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 3)
        {
            System.out.print("Input an amount to convert to Centimetres ");
            input = EasyIn.getInt();
            conversion = input * 2.54;
            System.out.println("Your input to Centimtres is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 4)
        {
            System.out.print("Input an amount to convert to Inches ");
            input = EasyIn.getInt();
            conversion = input * 0.393701;
            System.out.println("Your input to Inches is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 5)
        {
            System.out.print("Input an amount to convert to Kg ");
            input = EasyIn.getInt();
            conversion = input * 0.453592;
            System.out.println("Your input to Kgs is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 6)
        {
            System.out.print("Input an amount to convert to Pounds ");
            input = EasyIn.getInt();
            conversion = input * 2.20462;
            System.out.println("Your input to pounds is " + conversion);
            System.out.println("Press 7 to quit");
        }
    }while(number>=1 && number<7);


}

Comments

0

You are using index in your four loop, you can modify the loop like that:

while (number != 7) {
    // ...
}

You are showing the menu twice in the first iteration. Just one before the loop, and another one inside your loop. You can remove the code for showing the menu before the loop and initialize the value of the loop or use a do-while bucle.

do {
    // Show menu code
    // Read user action
    // Selected action code
} while (number != 7)

Comments

0

Instead of me correcting your answer, here is a better solution to your goal:

Instead of a for-loop, use a while-loop. The condition is while the input is not 7.

Tested and works:

int index = 0;
double conversion = 0;
int input = 0;

System.out.println("Enter 1 for Fahrenheit to Celius ");
System.out.println("Enter 2 for Celius to Fahrenheit ");
System.out.println("Enter 3 for Inches to Centimetres ");
System.out.println("Enter 4 for Centimetres to Inches ");
System.out.println("Enter 5 for Pounds to Kg ");
System.out.println("Enter 6 for Kg to Pounds ");
System.out.println("Enter 7 to quit ");

Scanner in = new Scanner(System.in);

index = in.nextInt();

while (index != 7)
{
    if (index == 1)
    {
        System.out.print("Input an amount to convert to Celsius ");
        input = in.nextInt();
        conversion = (5.0/9.0) * (input - 32);
        System.out.println("Your input to Celsius is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 2)
    {
        System.out.print("Input an amount to convert to Fahrenheit ");
        input = in.nextInt();
        conversion = input * 2.8;
        System.out.println("Your input to Fahrenheit is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 3)
    {
        System.out.print("Input an amount to convert to Centimetres ");
        input = in.nextInt();
        conversion = input * 2.54;
        System.out.println("Your input to Centimtres is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 4)
    {
        System.out.print("Input an amount to convert to Inches ");
        input = in.nextInt();
        conversion = input * 0.393701;
        System.out.println("Your input to Inches is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 5)
    {
        System.out.print("Input an amount to convert to Kg ");
        input = in.nextInt();
        conversion = input * 0.453592;
        System.out.println("Your input to Kgs is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 6)
    {
        System.out.print("Input an amount to convert to Pounds ");
        input = in.nextInt();
        conversion = input * 2.20462;
        System.out.println("Your input to pounds is " + conversion);
        System.out.println("Press 7 to quit");
    }
    index = in.nextInt();
}

Comments

0

If I understand the question correctly, the problem is that you're calling

number = EasyIn.getInt();

before your for loop, and then calling it a second time within your for loop. You're not actually doing any processing with the first call to EasyIn.getInt() so you're not seeing any results until the second call.

Pressing 7 is not going to cause your loop to exit because you never handle "7" once it's input.

I think you want something more like this:

while (true) {
    //do all your System.out.print() stuff
    number = EasyIn.getInt();

    // if conditions for 1, 2, 3, 4, 5, and 6

    if (number == 7) {
        return;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.