aspose file tools*
The moose likes Beginning Java and the fly likes switch ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "switch ??" Watch "switch ??" New topic
Author

switch ??

Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I am messing around with a program I made trying to test my knowledge of switch, but when I execute the following code, It does not print what it is supposed to.. What am I doing wrong here?..


Any help would be greatly appreciated...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

When you write a switch statement, it is always good practice to include a default block. If your added this:

to your statement you would see what the problem is. Have a look at the JavaDocs for InputStream's read() method: it is reading in bytes, not characters.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
aparna maddukuri
Greenhorn

Joined: Aug 21, 2006
Posts: 3
I too tried u'r problem.I got the same result as that of you.I think the problem is u'r reading the input from keyboard and then u'r pressing enter.Then it's taking both input+ascii value of enter(48).So there is no output.Just add the following 2 lines after reading input
System.out.println(month);
month=month-48;

regards
Aparna
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
Paul, thanks I will be checking into that, because I remember now reading about how it reads in bytes not characters.. I just learned getting input and switch, so I am going to stud a little more and see what I find..

Aparna, that code works up to number 9, then anything with a one in it shows January.. maybe you can't use too many cases in switch or something..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

Aparna, that code works up to number 9, then anything with a one in it shows January.. maybe you can't use too many cases in switch or something..

No, that's not the problem. The read() method reads just 1 byte. When you enter "12", there are two characters, not just one. You just read the first byte (the "1" of "12").

Try entering "24". It will print "February". Do you see why?

Instead of using System.in.read(), read the data another way, for example by using class Scanner:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
with System.in.read() are their parameters to modify how many byte you

want it to read??

Justin


You down with OOP? Yeah you know me!
Jay Suttiruttana
Ranch Hand

Joined: Aug 17, 2006
Posts: 36
You can also add these 3 lines of code, and it will solve your problem.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String data = br.readLine();
month = Integer.parseInt(data);

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch ??