Results 1 to 4 of 4
  1. #1
    student is offline Member
    Join Date
    Mar 2009
    Rep Power

    Default arryalist, while loop

    Hi, I have a problem with a program Im writing.
    I want the program to ask the user to input 10 sets of coordinates (so 2 integers per set).
    For this Ive made an arraylist to which I would like to add the sets of coordinates which have been entered by the user.
    The while loop should ask the players 10 times to input that set of coordinates and add each input to the next ite, in the arraylist.

    Can you guys tell me what I do wrong?
    Thanks to anyone who can help.

    Code of program
    import seabattle .*;
    import java.util.ArrayList;

    * Write a description of class Zeeslag here.
    * @author (your name)
    * @version (a version number or a date)

    public class Zeeslag
    private String player1;
    private String player2;
    Gui gui = new Gui(10 , 10);
    private int[] aantalSchepen;

    * constructor

    public Zeeslag(String speler1, String speler2)
    player1 = speler1;
    player2 = speler2;
    ArrayList aantalSchepen = new ArrayList();


    * methodes

    public void botenInvoeren(int x, int y)
    int index = 0;
    while(index < 10)
    aantalSchepen.add(new Position(index, (x,y) ));

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Rep Power


    Telling the forum what the problem you're seeing would help. What does it not do? Do you get compile errors? Wrong output? The more information you place about the problem you're seeing with your code, the more specific help you can get.

    Question: Where does the user input the information into the program?

    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    student is offline Member
    Join Date
    Mar 2009
    Rep Power


    One of the problems is that I want to put a couple of coordinates (x, y) in an Arraylist, and I don't know how to do this.

    I' ve added a screenshot which shows the error.
    Attached Thumbnails Attached Thumbnails arryalist, while loop-screenshot.jpg  

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    You declare aantalSchepen as an array of int (not an arraylist) in the class and as an ArrayList as a local variable in the constructor. Get rid of the declaration within the constructor as nothing else in the class can see it. Change the int array declaration in the class to that of an ArrayList.

Similar Threads

  1. for loop help
    By soc86 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 09:45 PM
  2. While loop
    By verbazon in forum New To Java
    Replies: 5
    Last Post: 02-25-2009, 01:29 AM
  3. Need help with a loop
    By ReV13 in forum New To Java
    Replies: 15
    Last Post: 12-01-2008, 04:48 PM
  4. For Loop
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 02:01 PM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 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