• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

switch ??

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
with System.in.read() are their parameters to modify how many byte you

want it to read??

Justin
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

 
reply
    Bookmark Topic Watch Topic
  • New Topic