[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

Java Using A, B, C, D to input data

I'm stuck with my homework project.  I don't what anyone to do my homework I need some help to get going.  I have to use A, B, C, D in my menu to order food.  Once A, B, C or D is entered is where my problem starts.  If the menu used numbers I could get it to work, but I'm have problems with the using characters.  Is there a way to convert the A, B, C or D to an integer so I can use "if statements."  I tried about 10 different ways, but I could get anything to work.  I keep getting an error "can't convert from char to Boolean".   Or is there a better way?  This is my 5th week in my Java class so please don't get too complicated.  Below is some requirement of the program and the code I have so far.

•      You should use named constants for food prices and tax rate, e.g.

Please enter your order (A, B, C, or D): e

Sorry, e is not a valid order
You have to select one of A, B, C, or D

Please enter your order (A, B, C, or D): t

Sorry, t is not a valid order
You have to select one of A, B, C, or D

Please enter your order (A, B, C, or D): d
How many: -2

Sorry, the quantity has to be at least 1
How many: 2



                                    
      

import java.util.Scanner;

	public class Assignment3Menu  {

		   public static void main(String[] args){

	    	
	    	  Scanner input = new Scanner(System.in);
	    	  
	    	  printWelcome();
	    	  printMenu(itemLetter, itemName, itemPrice);
	    	  getLetter();
	    	  
	    	  
		   } // ends main
		   
		   static  char[] itemLetter = {'A', 'B', 'C', 'D'};
	       static String[] itemName = {"Okinawa Soba", "Udon", "Ramen", "Coke"};
	       static String[] itemPrice = {"$5.45", "$4.49", "$7.79", "$0.95"};	

	    static void printMenu(char[] itemLetter, String[] itemName, String[]itemPrice){
	      	   for(int i = 0;i < itemLetter.length;i++) 
	      		System.out.printf("%-5c%-20s%-6s\n", itemLetter[i], itemName[i], itemPrice[i]);
	      System.out.println("\nEnter A, B, C or D. ");
	      	   
	    } // ends printMenu    	    	    	        
	 	static void printWelcome(){
	    System.out.println("Welcome to Walter's Ramen Shop.  "
	      		 	+ "Our menu is in English and our food is delicious.\n");
	    
	 	} // ends printWelcome
	 	
		 public static char getLetter(){
		     		        char ch;
		        Scanner scan = new Scanner(System.in);
		        ch = scan.next(). charAt(0);
		        	
		          System.out.println("\n" + ch);
		
		          return ch;
	        	
					        	
		 }   // ends getLetter	        
	        
		 	
       

         } // ends Assignment3Menu

Open in new window

0
WalterAPO
Asked:
WalterAPO
  • 4
  • 4
  • 3
  • +2
3 Solutions
 
for_yanCommented:



Yes you can convert them to integer in this way:

say
 String s is your input,

Then

int num = ("ABCD").indexOf(s);

will give you int num which will be equal to 0 if s is "A", 1 if "B", etc.
0
 
a_bCommented:
The first thing that you should is to convert the character to upper case in your program as 'A' differs from 'a'.

The other issue-

 public static char getLetter(){
                    char  ch;
                    Scanner scan = new Scanner(System.in);
                    ch = scan.nextLine(). charAt(0);
                    ch = Character.toUpperCase(ch);
                      System.out.println("\n" + ch);
            
                      return ch;
                    
                                            
             }   // ends getLetter             
0
 
for_yanCommented:
You rather return one-chracter String

		 public static String getLetter(){
		     		        char ch;
		       Scanner scan = new Scanner(System.in);
                                    String s = scan.nextLine().trim();

		    //    ch = scan.next(). charAt(0);
		        	
		          System.out.println("\n" + s);
		
		          return s;
	        	
					        	
		 }   // ends getLetter	      

Open in new window


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
for_yanCommented:

If you leave your method getLetter() as it is

You need only to change one line


 ch = scan.next(). charAt(0);

to this:
ch = scan.nextLine(). charAt(0);

then you can modify your main() methiod in this way
to determine which of the oprtions
was selected:




		   public static void main(String[] args){


	    	 // Scanner input = new Scanner(System.in);

	    	  printWelcome();
	    	  printMenu(itemLetter, itemName, itemPrice);
	    	  char ch = getLetter();

               int index = -1;
               for(int j=0; j<itemLetter.length; j++){
                   if(itemLetter[j] == Character.toUpperCase(ch)  ) index = j;
               }

               System.out.println("Menu number " + (index+1) + " was selected");


		   } // ends main

Open in new window


The it should all work together, like here:

Welcome to Walter's Ramen Shop.  Our menu is in English and our food is delicious.

A    Okinawa Soba        $5.45 
B    Udon                $4.49 
C    Ramen               $7.79 
D    Coke                $0.95 

Enter A, B, C or D. 
a

a
Menu number 1 was selected

Open in new window


0
 
CEHJCommented:
Only create the Scanner once, as the first statement in main. getLetter shouldn't have any println statement and the issue a_b mentioned. It should be as below. You can then do what follows
public static char getLetter() {
        return Character.toUpperCase(scan.nextLine().trim().charAt(0));
    } // ends getLetter
 
.....................


char entry = getLetter();
switch(entry) {
    case 'A':
     ...........
}

Open in new window

0
 
WalterAPOAuthor Commented:
 
I still am having a problem.  Once I get the A, B, C or D.  How do  I do the "if" statement and the math?  I still have to figure out the price and  using what was  inputted.  I can't get the below code to work.  The A, B, C, or D is really causing me problems.  

           if (s !=  'A', 'B', 'C', 'D');
                System.out.println("You have entered an incorrect letter."
                  + "Please enter A, B, C, or D." );
                
              else getLetter();
              
                


             if (s == 'A');
                   System.out.println("You orderd " + itemName[0]);

0
 
CEHJCommented:
You should use a switch statement as i mentioned

http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
0
 
a_bCommented:
The structure of your code looks incorrect -

    char s = getLetter();


    if (s !=  'A', 'B', 'C', 'D');
                System.out.println("You have entered an incorrect letter."
                  + "Please enter A, B, C, or D." );
               
    else
    {

         if (s == 'A');
                   System.out.println("You orderd " + itemName[0]);
          ............
   }

But instead of using the if else ladder, it would be best if use a switch case as suggested by CEHJ
0
 
WalterAPOAuthor Commented:
CEHJ,

I like the switch idea, but I show an error in the line of code below.   Eclipse states that scan can't  be resolved.  The quick fix was to change scan to Scanner and I got this error.  I don't know how to fix the error.

"Cannot make a static reference to the non-static method nextLine() from the type Scanner"


return Character.toUpperCase (scan.nextLine().trim().charAt(0));



Walter

0
 
CEHJCommented:
You need to define 'scan' as a global static variable (since your app is rudimentary)


public class Assignment3Menu  {

    static Scanner scan = new Scanner(System.in);
    .................
}

Open in new window

0
 
reijnemansCommented:
Hi,

Also could use enums to solve your problem: (reading the input is not in the example)

The enum:
public enum Answer {

	A("Okinawa Soba", "$5.45"), B("Udon", "$5.45"), C("Ramen", "$7.79"), D("Coke", "$0.95");

	private String name;
	private String price;
	
	private Answer(String name, String price) {
		this.name = name;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public String getPrice() {
		return price;
	}
}

Open in new window


handling the enum:
public static void main(String...args) {
		String charactar = "A";
		
		try {
			Answer answer = Answer.valueOf(charactar);
			System.out.println("You ordered: " + answer.getName() + " for " + answer.getPrice());
		} catch (IllegalArgumentException e) {
			System.out.println("Sorry, " + charactar + " is not a valid order, You have to select one of A, B, C, or D");
		}
		
	}

Open in new window

0
 
WalterAPOAuthor Commented:
Thank you for your help, but I was still having problems with using A, B, C, or D so I switched to numbers.  If I used letter I could only accept upper case and it was confusing trying to get the if statement to work.  I almost finished my project.  I might have to ask for some more assistance.  

Thank you,

Walter
0
 
CEHJCommented:
>>I switched to numbers.  If I used letter I could only accept upper case and it was confusing trying to get the if statement to work.

That's a pity - using numbers as suggested will make your code less readable/maintainable

The point of my comment was to get you using switch, which is more readable/maintainable than a lot of ugly if statements
0
 
WalterAPOAuthor Commented:
CEHJ,

Thank you for the help, but I just couldn't get the letters to work.  If I have more time I would keep the number, but my assignment is due today.  If I have points I need help with my methods.  

Walter
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now