• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch ??

 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
aparna maddukuri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with System.in.read() are their parameters to modify how many byte you

want it to read??

Justin
 
Jay Suttiruttana
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic