Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-28
14
Medium Priority
?
207 Views
Last Modified: 2012-05-12
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
Comment
Question by:WalterAPO
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 668 total points
ID: 36714986



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
 
LVL 20

Expert Comment

by:a_b
ID: 36714997
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36714998
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36715041

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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1332 total points
ID: 36715784
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
 

Author Comment

by:WalterAPO
ID: 36715882
 
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36715947
You should use a switch statement as i mentioned

http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
0
 
LVL 20

Expert Comment

by:a_b
ID: 36716011
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
 

Author Comment

by:WalterAPO
ID: 36716129
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1332 total points
ID: 36716176
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
 
LVL 4

Expert Comment

by:reijnemans
ID: 36813163
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
 

Author Closing Comment

by:WalterAPO
ID: 36889929
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36890214
>>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
 

Author Comment

by:WalterAPO
ID: 36895369
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question