1

I am building an Interactive Fiction game that uses a second class to deal with commands. When I type a command it is sent to the second class to be processed. The problem is when I run the code my values (int x and int y) are not being passed back into the main class. How do I pass these values back into the main class so that northD will print?

Main class:

import java.util.Scanner;
import static java.lang.System.out;

    public class Main {

        static String cmdIF;
        static int x = 0;
        static int y = 0;
        static String northD;
        public static void main(String[] args) {

            Scanner input = new Scanner(System.in);

            out.println("Welcome to the world! Which way do you want to go?");
            cmdIF = input.nextLine();
            choosePath();
            if(x==1 || y == 0) {
             northD = "You have entered the woods.";
                out.print(northD);
            }
        }
        public static void choosePath() {
            actionClass.cmdCenter(cmdIF, x, y);
        }
    }

Second class:

import static java.lang.System.out;
public class actionClass {
 public static void cmdCenter(String cmdIF, int x, int y) {
     if(cmdIF.equalsIgnoreCase("NORTH") || cmdIF.equalsIgnoreCase("GO NORTH")){ 
      x++;
     }
     else if(cmdIF.equalsIgnoreCase("EAST") || cmdIF.equalsIgnoreCase("GO EAST")) {
      y++; 
      }
     else if(cmdIF.equalsIgnoreCase("SOUTH") || cmdIF.equalsIgnoreCase("GO SOUTH")) { 
      x--; 
      }
     else if(cmdIF.equalsIgnoreCase("WEST") || cmdIF.equalsIgnoreCase("GO WEST")) { 
      y--; 
      }
     else { out.println("You can't do that."); }
 }
}
SputnicK
  • 103
  • 1
  • 10

3 Answers3

1

You can return an array of integer (or better an object). This the simplest way (in my opinion)

Main class:

public class Main {

static String cmdIF;
static int x = 0;
static int y = 0;
static String northD;
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Welcome to the world! Which way do you want to go?");
    cmdIF = input.nextLine();
    choosePath();
    if(x==1 || y == 0) {
     northD = "You have entered the woods.";
        System.out.println(northD);
    } else {
        System.out.println("You have entered the home");
    }
}
public static void choosePath() {
    //Method return an array of integer
    int[] newPos = actionClass.cmdCenter(cmdIF, x, y);
    //GETX
    System.out.println(newPos[0]);
    //GETY
    System.out.println(newPos[1]);
    //set X
    x = newPos[0];
    //set Y
    y = newPos[1];
}
}

Activity class:

public class actionClass {
  public static int[] cmdCenter(String cmdIF, int x, int y) {
   if(cmdIF.equalsIgnoreCase("NORTH") || cmdIF.equalsIgnoreCase("GO NORTH")){ 
    x++;
 }
   else if(cmdIF.equalsIgnoreCase("EAST") || cmdIF.equalsIgnoreCase("GO EAST")) {
    y++; 
  }
   else if(cmdIF.equalsIgnoreCase("SOUTH") || cmdIF.equalsIgnoreCase("GO SOUTH")) { 
    x--; 
  }
   else if(cmdIF.equalsIgnoreCase("WEST") || cmdIF.equalsIgnoreCase("GO WEST")) { 
    y--; 
  }
   else { System.out.println("You can't do that."); }

 //New array, first position x, second position y
 int[] res = {x,y};
 //Return it
 return res;
}

}

Output:

Welcome to the world! Which way do you want to go?
EAST
0
1
You have entered the home

More output:

Welcome to the world! Which way do you want to go?
NORTH
1
0
You have entered the woods.

With an object

A more complex way is to use a custom object. So new class:

public class xyObj {

    public int x;
    public int y;

    //Set x and y
    public xyObj(int x,int y){
        this.x=x;
        this.y=y;
    }
    //get x
    public int getX(){
        return x;
    }
    //get y
    public int getY(){
        return y;
    }
}

Now activity class return this object:

public class actionClass {
 public static xyObj cmdCenter(String cmdIF, int x, int y) {
     if(cmdIF.equalsIgnoreCase("NORTH") || cmdIF.equalsIgnoreCase("GO NORTH")){ 
      x++;
     }
     else if(cmdIF.equalsIgnoreCase("EAST") || cmdIF.equalsIgnoreCase("GO EAST")) {
      y++; 
      }
     else if(cmdIF.equalsIgnoreCase("SOUTH") || cmdIF.equalsIgnoreCase("GO SOUTH")) { 
      x--; 
      }
     else if(cmdIF.equalsIgnoreCase("WEST") || cmdIF.equalsIgnoreCase("GO WEST")) { 
      y--; 
      }
     else { System.out.println("You can't do that."); }

     //new xyObj setting x and y
     xyObj ret = new xyObj(x, y);
     //return it
     return ret;
 }

}

We have to modify also the choosePath method

public static void choosePath() {
    xyObj xyPos = actionClass.cmdCenter(cmdIF, x, y);
    System.out.println(xyPos.getX());
    System.out.println(xyPos.getY());
    x = xyPos.getX();
    y = xyPos.getX();
}

Same output! Good luck!

Francesco Serra
  • 763
  • 8
  • 13
  • A class or struct of some sort would probably make more sense than an array. – David May 20 '16 at 16:18
  • Yes is also possible define a new class (x,y as datamember) and return it – Francesco Serra May 20 '16 at 16:19
  • @Francesco Serra Thanks so much for the reponse! I added the two lines of code in actionClass() and added the int[] xy to the choosePath() method like you said and this error happened: `Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from void to int[] at Main.choosePath(Main.java:25) at Main.main(Main.java:16) ` – SputnicK May 20 '16 at 16:31
  • have you canged the method definition?? public static int[] cmdCenter(String cmdIF, int x, int y) – Francesco Serra May 20 '16 at 17:25
  • @FrancescoSerra Yes I did. I copied/pasted your code just to make sure and I still get the same error. – SputnicK May 20 '16 at 19:01
  • I've tested it and there is no problem ... I will edit my answer with all the code. – Francesco Serra May 20 '16 at 19:40
  • Thank you that worked! Very good explanation and code - I will definitely experiment with both solutions :) – SputnicK May 21 '16 at 13:34
1

So in method cmdCenter(...) you will add a return statement like this:

 public static int[] cmdCenter(String cmdIF, int x, int y) {
       .....//your code

       return new int[]{x,y};
  }

which will return an array containing x at cell 0 and y at cell 1

So basically your method is public,static,not void(it returns and int[] [integer array] etc..)

*Check out how to use return statement to in java here

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • I receive this error upon typing "NORTH" in the console: `Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot define dimension expressions when an array initializer is provided at actionClass.cmdCenter(actionClass.java:18) at Main.choosePath(Main.java:24) at Main.main(Main.java:16) ` – SputnicK May 20 '16 at 16:41
1

This is not possible for primitive type (int, long, ...) because they are passed by value.

OOP solution would be to encapsulate your variable into an object.

An other solution you can use here is to refer to the static variable into the cmdCenter.

Maybe this post may help you to understand.

Community
  • 1
  • 1
Alexandre Cartapanis
  • 1,513
  • 3
  • 15
  • 19
  • This is the correct answer and explains rather than does the work for the question asker. – Christopher Schneider May 20 '16 at 16:32
  • The post was very helpful. - I'm still confused how I pass the value `int x` back into the main method though. How would I encapsulate the variable into an object? – SputnicK May 20 '16 at 16:51