Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

help with Switch Statement

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can someone help me on converting the following Ruby switch Statement into a Java switch statement?




Thank you in advance for your help!
 
lowercase baba
Posts: 12838
52
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried? Do you know what a basic Java switch statement looks like?
 
John Marko
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:What have you tried? Do you know what a basic Java switch statement looks like?




Thank Fred for your swift reply!

I have tried the following but its totally wrong


 
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with it?
Are you getting compilation errors?  If so, then what are they and what line(s) do they occur on?

If not, then what is the issue?
 
Marshal
Posts: 69033
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your posts: they even have an option for Ruby highlighting. Always use the tags: doesn't it look better
I would suggest you shou‍ld not try to translate the switch statement from one language to another. For one thing, you get variable names with _ in which is not the normal Java® style. For another, there seems to be confusion about types; in the Ruby you appear to be passing what looks like arrays, which in the Java® you have turned into Strings. I suggest you write down on paper what the switch statement is supposed to do, then create a new switch (or other statement) in Java® without looking at the Ruby code.

I am moving this discussion as too difficult for this forum.
 
fred rosenberger
lowercase baba
Posts: 12838
52
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell makes a good point. Before you can convert your Ruby into Java, you have to understand what your Ruby code is doing.

this is not valid. I'm not sure what you are trying to do here:


So stop. write out in English (or your natural lagunage of choice) what you are trying to accomplish. THEN start trying to code it.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic