2 Replies - 297 Views - Last Post: 29 November 2012 - 03:42 PM Rate Topic: -----

#1 nickelz429  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 13-September 12

problem with do while loop and if, else if statements

Posted 29 November 2012 - 02:15 PM

i have a program that asks the user to select an option. everything works fine but when i chose selection A and am finished with it. It asks me to make another selection A,B, or C but no matter what i enter it automatically chooses selection A. I do not get this problem when selecting option B, C or any other selection.
// Nick Mendoza
// Lab6
// Chapter 11 Ex4

import java.util.Scanner;

public class testLAB6
{
	public static void main (String [] args)
	{
		System.out.print("Please chose an input option: ");
		System.out.println("\n	A	Month by Number");
		System.out.println("	B	Month by Name");
		System.out.println("	C	Exit");

		Scanner kb = new Scanner (System.in);
		String input = kb.nextLine();
		input = input.toUpperCase();
		char ans = input.charAt(0);


		do
		{
			if(ans == 'A')
			{
				System.out.println("You chose option A");
				System.out.print("Please enter the month by number: ");
				int n = kb.nextInt();
				kb.nextLine();

		// TRY/CATCH to throw InvalidMonthNumberException
				try
				{
					Month test = new Month(n);
					System.out.println(test);
					
				}
				catch(InvalidMonthNumberException e)
				{
					System.out.println(e.getMessage());
				}
				finally
				{
					System.out.println("FINALLY");
				}
				System.out.println("OUT OF TRY / CATCH");
				
			}
			else if(ans == 'B')
			{
				System.out.println("You chose option B");
				System.out.println("Please enter the month by name: ");
				String name = kb.nextLine();

		// TRY/CATCH to throw InvalidMonthNameException
				try
				{
					Month test = new Month(name);
					System.out.println(test);
				}
				catch(InvalidMonthNameException e)
				{
					System.out.println(e.getMessage());
				}
				finally
				{
					System.out.println("FINALLY");
				}
				System.out.println("OUT OF TRY / CATCH");
			}
			else if(ans == 'C')
			{
				System.out.println("You chose option C");
				System.out.println("**** GOOD BYE ****");
			}
			else
			System.out.println("**** INVALID SELECTION ****");
			System.out.print("Please chose another option. ");
			input = kb.nextLine();
			input = input.toUpperCase();



		}while(ans != 'C');
        }
}




Is This A Good Question/Topic? 0
  • +

Replies To: problem with do while loop and if, else if statements

#2 johnml  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-September 12

Re: problem with do while loop and if, else if statements

Posted 29 November 2012 - 03:40 PM

Well, I'm rather beginner than specialist, but I will try to help.

You check the 'ans' variable, which will not be changed for all time of running the program.
So you put A, and after doing do-while part still will be A..
Try to put your menu and scanner for it to do-while loop.
char ans;
	do
        {
	  //Your menu

	  Scanner kb = new Scanner (System.in);
	  String input = kb.nextLine();
	  input = input.toUpperCase();
	  ans = input.charAt(0);

          //All ifs

       }while(ans != 'C');



I think that's what you need. I don't know only why you writed that for B it was working ok.
You should better wait for someone more experienced who would say that my solution is ok, or give you another answer.
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: problem with do while loop and if, else if statements

Posted 29 November 2012 - 03:42 PM

johnml did fine, but I'll say it a little differently:

Check the do loop and look for where the variable 'ans' will change. Find it?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1