public Cube(String id, String faceValues){
this.id = id;
id.toUpperCase();
for (int i =0; i <6; i++){
faces[i]=faceValues.charAt(i);
}
faceIndex = 0;
rnd = new Random(0);
}
What I'm trying to do is obtain input from the user and set their input as the 'String id' parameter in the above Cube(String id, String faceValues) method:
Code:
import java.util.Scanner;
public class CubeTester {
//Instance variables
private Cube c1;
private Cube c2;
private Cube c3;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//Constructor
Cube c1 = new Cube();
Cube c2 = new Cube();
Cube c3 = new Cube();
//Ask for String id
System.out.println("Enter cube id: ");
scan.next(); // I want to set this as String id in the Cube method
//Then ask for the String faceValue
System.out.println("Enter 6 letters for faces of the cube: ");
scan.next(); //I want to set this a the String faceValue in the Cube method
}
}
Second, every time you call scan.next() it reads from system.in. so if you do something like
Code:
int i = 0;
String[] values = new String[6];
while(i < 6) {
values[i] = scan.next();
i++;
}
You'll get a string[] with the 6 entries from System.in Then you can initialize the cubes likes this:
Code:
Cube c1 = new Cube(values[0],values[1]);
Cube c2 = new Cube(values[2],values[3]);
Cube c3 = new Cube(values[4],values[5]);
Ah, thank you. I've modified my code to understand a little better:
Code:
import java.util.Scanner;
public class CubeTester {
//Instance variables
private Cube c1;
private Cube c2;
private Cube c3;
public static void main(String[] args) {
//Constructor
Cube c1 = new Cube();
Cube c2 = new Cube();
Cube c3 = new Cube();
}
private Cube createCube (Scanner keyboard){
System.out.println();
System.out.println("Enter cube id: "); //Ask for String id
String id = keyboard.next(); // I want to set this as String id in the Cube method
System.out.println("Enter 6 letters for faces of the cube: "); //Then ask for the String faceValue
String faceVal = keyboard.next(); //I want to set this a the String faceValue in the Cube method
return c1;
}
}
Bookmarks