• 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

Identify shapes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats what I'm trying to do
If user inputs 3, onscreen message should say "A triangle has 3 sides."  
*     If user inputs 4, onscreen message should say "A rectangle has 4 sides."
*     If user inputs 5, onscreen message should see "A pentagon has 5 sides."

and here is my java code





 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you have 1, 2, and 5 when your requirements state 3, 4, 5?
 
Yossry Helal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed it into 3,4,5 but still didnt work.
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that this is part of your requirements, this calls for an 'if' not a 'switch'. Do you know what the correct range is?
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How weird... When using a switch why would you check if the users input is out of bounds? Add a default, when the user is out of bounds the default message is displayed.
Like Carey already pointed out you're using '1', '2' and '5' in your switch while it should be "3" "4" and "5" and you're comparing a number to a string, so it should be case 3, 4 and 5.
Your if statements are totally useless and use the userInputStringOfAngle which is a string yet they compare it to a number.
To compare strings don't use == but use the .equals("") method. The .equals("") method compares the value of the string, when using == java compares the objects, not the value.
So:

And finally, your variablenames are verry clear, I give you that, but still a bit long for my taste, for example: myInputScannerInstance, whats wrong with input or simple scanner.

To demonstrate what I ment with the default:


I also see you taking in a string just to parse it to a number, if you're sure the user will always put in a number you could take in the int directly.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always tell everybody what the question is about in its title.
Please have a look at your variable names, “week2” is a bad name for a class and it should start with a CapitalLetter, too. I think the name for your Scanner is too long: maybe “input” would be better, but you can probably think of a better name.
Why mess around with parseInt() when your Scaanner can read an int directly. There is an example in its documentation.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you parse numberOfAngles as an int, then your switch statement should use ints, not Strings:
For example,
should just be
reply
    Bookmark Topic Watch Topic
  • New Topic