Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

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

Posted on 2011-09-28
Medium Priority
205 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
``````
0
Question by:WalterAPO
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 4
• 4
• 3
• +2
14 Comments

LVL 47

Accepted Solution

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

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

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
``````

0

LVL 47

Expert Comment

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
``````

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
``````

0

LVL 86

Assisted Solution

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':
...........
}
``````
0

Author Comment

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

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

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

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

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);
.................
}
``````
0

LVL 4

Expert Comment

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;
}
}
``````

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");
}

}
``````
0

Author Closing Comment

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

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

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

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
###### Suggested Courses
Course of the Month9 days, 2 hours left to enroll

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

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