Results 1 to 6 of 6
  1. #1
    jepoy is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default how to ask how many arrays you want

    Hello! I'm new to java and im trying to figure out how to use a scanner for how much arrays you can select.
    I'm browsing through the forum and I'm pretty amazed how active and friendly the java community is.
    Anyways i have a file named Rectangle.java where all it does is calculate the area of the given input from the scanner (just width * length)
    Then i have this RectangleMenu.java please have a look

    Java Code:
    import java.util.Scanner;
    
    public class RectangleMenu {
    
    	double input1, input2;
    	
    	Rectangle[] recArray;
    	
    	public RectangleMenu () {
    	
    		input1 = 0;
    		input2 = 0;
    
    	}
    	
    		public void run() {   
    			
    			recArray = new Rectangle[3];
    			Scanner fred = new Scanner(System.in);
    		
    	
    			
    			System.out.println("Input Width for Rectangle");
    			input1 = fred.nextDouble();
    			System.out.println("Input Length for Rectangle");
    			input2 = fred.nextDouble();
    			recArray[0] = new Rectangle(input1, input2);
    		
    			
    			System.out.println("Input Width for Rectangle2");
    			input1 = fred.nextDouble();
    			System.out.println("Input Length for Rectangle2");
    			input2 = fred.nextDouble();
    			recArray[1] = new Rectangle(input1, input2);
    	
    			System.out.println("Input Width for Rectangle3");
    			input1 = fred.nextDouble();
    			System.out.println("Input Length for Rectangle3");
    			input2 = fred.nextDouble();
    			recArray[2] = new Rectangle(input1, input2);
    			
    		
    			System.out.println(recArray[0].getArea());
    			System.out.println(recArray[1].getArea());
    			System.out.println(recArray[2].getArea());
    		}
    		
    		}
    		
    	}
    Its all working good, just wondering how can i use a scanner that ask "how many rectangles do you want? 1? 2? or 3?" since i got 3 arrays. I think i need to use a while loop, but I'm so new that i failed so many times, not to mention my lack of IQ
    Thanks again
    Last edited by jepoy; 09-06-2010 at 12:36 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I would ask how many rectangles the user wants first, and then use a for loop since a for loop is a better loop to use when you know in advance (before the loop starts) how many times the loop will occur. My suggestion to you is that you go ahead and try to do this using a for loop first on your own, then if it doesn't work, you can post the attempt here and we can work with you on it.

    Best of luck with this project and with your Java education!

  3. #3
    jepoy is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you for the reply Fubarable

    I dont think for loops is gonna be effective because i manually did the
    Java Code:
    for(Rectangle i = 0; i < array ; i++) {
    for this one by copying and pasting each arrays...
    is it possible to select how many arrays you want? out of [3] i created?

    i tried making a 2nd scanner but it wont work
    perhaps i need a while loop?

    can u give me a hint please?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The for loop will work but the index inside the loop needs to be an int, not a "Rectangle". You only need one Scanner. Something like:


    Java Code:
    // semi-pseudocode
    prompt user for how many rectangles
    get int called rectCount from user using scanner
    create Rectangle array, recArray, that is [rectCount] size
    for (int i = 0; i < rectCount; i++) {
      // do whatever needs to be done inside the loop!
      get user input and with it create recArray[i]
    }

  5. #5
    jepoy is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you very much

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  3. Arrays
    By tejens23 in forum New To Java
    Replies: 5
    Last Post: 11-25-2009, 11:46 PM
  4. Help with arrays
    By Skorge in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 08:35 AM
  5. Arrays
    By hypes057 in forum New To Java
    Replies: 13
    Last Post: 09-04-2009, 11:40 AM

Posting Permissions

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