This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Something is missing in my Switch method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Something is missing in my Switch method" Watch "Something is missing in my Switch method" New topic
Author

Something is missing in my Switch method

Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128






the instruction to this code is to make the user write numbers from 1-100 and the application should categorize the numbers in the following way



100 - A
90-99 - B
80-89 - C
70-79 - D
60-69 - E
other - F


the problem is with the other. it should go from 59 to 0.

however, the application categorizes any number as "others"

how can i cause the application to accept numbers from 0-100 and not accept numbers higher than 100? [/code]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Use a series of if-statements:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
David Newton wrote:Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.



This exercise is only on the switch function. you are not allowed to use if or while or any other fancy method..


i think i am close to the answer, but something is missing to make the "other" -10 to 50. alternatively , make the input go to 100 and not above
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you have certain requirements for a particular problem, you need to state them up front--how else can we be expected to help in any reasonable way?

So you have at least a couple of options: you could do some match to make the switch statement work without listing all the numbers, or you could just list all the numbers.

Do you have any *other* requirements we need to know?
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
Dmitri Makovetskiy wrote:
David Newton wrote:Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.



This exercise is only on the switch function. you are not allowed to use if or while or any other fancy method..


i think i am close to the answer, but something is missing to make the "other" -10 to 50. alternatively , make the input go to 100 and not above




why cant i write before the case 60:




and at the end

default:
System.out.println("invalid number");
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
David no. There is a line that i need to add, i think it should prevent the numbers going over 100.

the way i listed above, is a bit unwanted, cause i was instructed to make 5 cases and not 10...
but if i have no choice, i will have to go with the way above.

is there a way to make the numbers go till 100?


if i put somewhere near the end this statement:
number=100

would it work?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You need to check the number for validity *before* entering the switch statement.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3011
    
  34

If you want to do this just with a switch block, you need to specify 100-69 = 31 case items.
If not, you should use some conditions like if-checks or ternary-if-checks.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(@Devaka: not necessarily--check the math in the original post.)
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3011
    
  34

David Newton wrote:(@Devaka: not necessarily--check the math in the original post.)

Oh ya, thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Something is missing in my Switch method
 
Similar Threads
this problem baffles me
How to compare String and int?
Test score
Netbeans and Ctrl + z (end of file)
why does it tell me that there is else without if (java programming)?