Translating Ruby to Java : Does Java have a construct that is close to the Ruby switch statement

I'm trying to translate what the code I have, in Ruby, to Java.

So far I have been unable to come across a switch statement-type construct in Java that will let me call methods in the cases.

Does anyone have any suggestions for a replacement construct that performs similar to the code below.

def move_validator message = nil case when out_of_bounds?(@x_dest, @y_dest) == true message = "You cannot move off the board" when no_checker_at_origin? == true message = "There is no checker to move in requested location" when trying_to_move_opponents_checker? == true message = "You cannot move an opponents checker" when trying_to_move_more_than_one_space_and_not_jumping? == true message = "You cannot move more than one space if not jumping" when attempted_non_diagonal_move? == true message = "You can only move a checker diagonally" when attempted_move_to_occupied_square? == true message = "You cannot move to an occupied square" when non_king_moving_backwards? == true message = "A non-king checker cannot move backwards" when attempted_jump_of_empty_space? == true message = "You cannot jump an empty space" when attempted_jump_of_own_checker? == true message = "You cannot jump a checker of your own color" when jump_available_and_not_taken? == true message = "You must jump if a jump is available" else move if jumping_move? message = "jumping move" remove_jumped_checker end king_checkers_if_necessary end message end


-------------Problems Reply------------


else if( condition)

Downside is you will have to manually optimize the order in which they go.


if (outOfBound(y, y)) {
message = "...";
} else if (attemptedNonDiagonalMove()) {
message = "...";
} else {

Or make every condition an object of your own Condition class with a test() and a getMessage() and have a List. Problem are the x, y parameters.

Category:java Views:8 Time:2011-11-02

Related post

  • Ruby: More flexibility than Java/C#? 2009-01-01

    Is is that I'm a newbie learning Ruby, or does it really have more ways to write (the same) things than Java/C#? Also, if it is more flexible than Java, are there any linguistic features of Ruby that are generally not used to avoid confusion? Example

  • How to parse/leach/extract a website Content using Ruby or PHP or Java? 2009-09-17

    I am doing a hobby project to scrape the content of an website using Ruby or PHP or Java . For example if the website url "" . i would like to extract the unicode text content from home.aspx and paste it to a notepa

  • How to get started with Ruby programming language with Java background? 2010-01-21

    Hi I am intermediate in java but as one of the company requirements they are looking for JAVA+RUBY programming language.. Can anybody help how to get started with Ruby I need to get it done in next one month so that I can crack the interview of the c

  • Translate Spring XML configuration to Java config 2010-02-10

    I'd like to use Spring with Java configuration, but almost all examples are writte in XML and I don't know how to translate them to Java. Look at these examples from Spring Security 3: <http auto-config='true'> <intercept-url pattern="/**" a

  • If Java people go to Scala, C# go to F#, where do Ruby people go for functional nirvana? 2010-02-12

    I know a lot of Java people have started looking at Scala since it runs on the JVM, and a lot of people in the Microsoft world are looking at F#, but what does Ruby have as a natural functional successor? In a pure FP sense Ruby doesn't lack anything

  • Should I try to reuse Ruby code in a Java project? 2010-02-24

    I'd rather not "reinvent the wheel" and I've found a Ruby project that provides functionality I need in my Java app (there is no preexisting Java project that does what I need - trust me, I've looked). So, best case scenario, I am able to run the Rub

  • Need help in translating code from C to Java 2010-05-11

    From this article. Here's the code: float InvSqrt(float x){ // line 0 float xhalf = 0.5f * x; int i = *(int*)&x; // store floating-point bits in integer i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method x = *(float*)&i; //

  • How to get from JRuby a correctly typed ruby implementation of a Java interface? 2011-01-06

    I'm trying to use JRuby (through the JSR233 interface included in JRuby 1.5) from a Java application to load a ruby implementation of a Java interface. My sample implementation looks like this: Interface: package some.package; import java.util.List;

  • Ruby Savon compared to Java Axis 2011-11-07

    I'm trying to use the Yodlee soap APIs. They give examples with Apache Axis Any perspectives on using Ruby and Savon versus Java and Axis? With Ruby/Savon, it seems that the best thing to do is to skip using the WSDL files and generate the requests b

  • how to translate curl -X post into java 2012-02-01

    I am trying to translate a curl command into Java (using Apache HttpClient 4.x): export APPLICATION_ID=SOME_ID export REST_API_KEY=SOME_KEY curl -i -X POST \ -H "X-Parse-Application-Id: ${APPLICATION_ID}" \ -H "X-Parse-REST-API-Key: ${REST_API_KEY}"

  • Ruby Gem works on commandline and irb - fails when run as ruby script or via Java Process 2012-03-26

    I've been trying to get a certain Google API to work for two days now. Three different projects have been tried - one in Perl (which died very early on) one in Python (crashed and burnt at the last hurdle) and finally one in Ruby. The Ruby one actual

  • correct this boolean translation expression from cpp to java 2014-08-08

    Is this boolean translation expression from cpp to java correct? cpp block[j*256+i] = (((j/4 & 15) == 0) || (((i/4 & 15) == 0) && ((((i/4 & 31) == 0) ^ ((j/4 >> 4) & 1)) == 0))) ? Color(60,60,60,255) : Color(180,180,180,

  • RAD webservice generater and *, * 2009-02-26

    When i generate a webservice from my DAO in RAD it creates a, and classes. Then when i put this code into my CVS(Clearcase) i notice that these files get hijacked when a call is made to my service. Is there a way to av

  • What is the .NET equivalent of Java's java.util.concurrent package? 2009-11-30

    I come from a Java background. I am wanting to learn more about concurrency in .Net and C#. Is there something similar to Java's concurrent utils package? --------------Solutions------------- The closest will be the new threading abilities in .NET 4.

  • Real time object tracking in java(some java API) or C#(emgucv,dshownet,Aforge.NET) 2010-05-26

    I am doing a project called user initiated real time object tracking system. Here, is what I want to happen in the project: 1) Take a continuous stream from a web camera. 2) Using the mouse a user can draw a square, around an object of interest. 3) T

  • Using Castor, how do you map Java class "java.util.ArrayList" to element "ArrayList", but also generate elements for the objects that it contains? 2010-06-09

    Using Castor, how do you map Java class java.util.ArrayList to element <ArrayList/> instead of <array-list/> while still including the elements that it contains? For example, the class mapping <class name="java.util.ArrayList"> <

  • Difference between Java SE & Java EE 2010-08-08

    This question already has an answer here: Difference between Java SE/EE/ME? 8 answers What is the difference between Java SE & Java EE? --------------Solutions------------- Java SE (standard edition) is just the normal specification of Java. Java

  • Java equivalent of Python's "construct" library 2011-03-06

    Is there a Java equivalent of Python's "construct" library? I want to write "structs" like so: message = Struct("message", UBInt8("protocol"), UBInt16("length"), MetaField("data", lambda ctx: ctx["length"]) ) It doesn't have to specifically be a libr

  • Differences between Jakarta Regexp and Java 6 java.util.regex 2011-07-05

    I am in the process of migrating from Jakarta Regexp to the standard Java 6 regular expressions package java.util.regex. I noticed the following difference when not specifying the beginning ^ and end $ in a regexp: Jakarta Regexp returns true when th

  • Java to Java Bytecode Compiler: How Do I Get Java Class Library Information? 2012-02-14

    I'm writing a Java to Java Bytecode compiler by compiling to Jasmin code, and assembling that. I'm creating it with the intention that source code files will be able to reference and use Java Library classes (you pretty much have to for Strings, prin

Copyright (C), All Rights Reserved.

processed in 0.185 (s). 11 q(s)