www.webdeveloper.com
Results 1 to 3 of 3

Thread: Parameter/Argument Method Passing Question

Hybrid View

  1. #1
    Join Date
    Sep 2008
    Posts
    13

    Parameter/Argument Method Passing Question

    Hello,
    I have a method:
    Code:
      	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
    	}
    }
    Any help would be greatly appreciated.

    Thanks,

    n

  2. #2
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    First, you have a constructor, not a 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]);

  3. #3
    Join Date
    Sep 2008
    Posts
    13
    Quote Originally Posted by chazzy View Post
    First, you have a constructor, not a 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;
    		}
    	
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles