what is the problem with my code?

where is the problom with my code?

Page 1 of 1

1 Replies - 535 Views - Last Post: 30 March 2010 - 01:17 AM Rate Topic: -----

#1 ksaihat  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 11-March 10

what is the problem with my code?

Posted 30 March 2010 - 01:08 AM

Hi
my program is presenting menu which present a list of avialable option(A-B-C)
if the user enter an invalid option the program display error massage
The problem is where i enter the invalide choice 2 times in while loop or when i enter available choice
in the firest time then i chose invalide choice




import java.io.IOException;
import java.text.*;
import java.util.*;
public class choice {
Scanner reader=new Scanner(System.in);
		char choice ='\0';
        do
        {
                System.out.println( Menu");
        	    System.out.println("A.Checking ");
                System.out.println("B. saving .");
                System.out.println("C.Exiting .");
                System.out.print(" enter choice ");
                
                try
                {
                        choice = (char)System.in.read();
                        System.in.read(); //eat the '\n'
                     
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                }

                if(choice=='A' || choice == 'a'){
                        
                            System.out.println("A");
                         
                }
                
                else if(choice=='B' || choice == 'b') {
                    
                    System.out.print("b:");
                    
                }
                
                else if (choice == 'C' || choice == 'c') {
                        System.out.println("exit");
                }
                
                else {
                        System.out.println("Error!"+" "+ choice +" "+"is not a valid  option!");
                
                }
                System.out.println();
        }while (choice != 'C' && choice != 'c');


	}
	
}



This is the output


 Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice:a

A

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice:b

B


Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice: r

Error! r is not a valid option!
Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice: o

Error!
 is not a valid menu option!

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice:
Error!
 is not a valid menu option!

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice: w

Error! w is not a valid menu option!

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice: w

Error!
 is not a valid menu option!

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice: (i print c put it dose not appeare)
Error!
 is not a valid menu option!

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice: r

Error! r is not a valid menu option!

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice: C

Error!
 is not a valid menu option!

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice:
Error!
 is not a valid menu option!

Menu
A.Checking
B.Basic saving calculator.
X.Exit the program.

enter choice:C

exiting


Is This A Good Question/Topic? 0
  • +

Replies To: what is the problem with my code?

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: what is the problem with my code?

Posted 30 March 2010 - 01:17 AM

Dont know what your are exactly saying you error is, but take a look at the while part of your loop. You say, while it is not equals to C AND it is not equal to c. So, it will loop until both a captial C and lower case c are pressed at the same time, which i dont think is possible. Did you mean to use OR here?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1