// 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');
}
}
2 Replies - 99 Views - Last Post: 29 November 2012 - 03:42 PM
#1
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.
Replies To: problem with do while loop and if, else if statements
#2
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.
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.
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.
#3
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?
Check the do loop and look for where the variable 'ans' will change. Find it?
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|