0

How do I, if it's possible, take in user input to fill an ArrayList<Integer>?

This is what I have so far, but the code doesn't let me incorporate user input. I'm having a hard time figuring this one out...

import java.util.*;

public class Duplicates 
{
	Scanner input = new Scanner( System.in );
	int i;
	int cnt = 0;
	ArrayList<Integer> numbers = new ArrayList<Integer>(5);
	
	
	public void EnterNumbers()
	{
		System.out.println("Please Enter 5 variables: ");
		for (i=0; i<=4; i++)
		{
			System.out.printf("Please enter value in slot " + i + ":");
			numbers.add(i);
			cnt++;
		}
		System.out.printf(" = " + cnt+ "\n");
	}
	
	
	public static void main(String[] args)
	{
		Duplicates DuplicateTest = new Duplicates();
		
		DuplicateTest.EnterNumbers();
	}
}
2
Contributors
3
Replies
13
Views
6 Years
Discussion Span
Last Post by eman 22
0
for (i=0; i<=4; i++)
		{
			System.out.printf("Please enter value in slot " + i + ":");
// add this line
                        int j=input.nextInt();
			numbers.add(j);
			cnt++;
		}

try this .
the line
int j=input.nextInt();
makes you to take input from user
good luck

0

Ok....how would I then compare values that are in the array, and remove any doubles (keeing the original and remove the double)?

0

to do that, you should think how can you do in reality.I think it's do by comparing each elemnet with the other elements and if there are doubles delete one of them.
to make the perviouse way in java try to write the code of the following quote.

after adding the elements to this array, you should make two for loop on array .
first for choose a specific element and then compare this with the rest of elements in the array, if two elements are equal delete one of them and reduce the size of array

I hope I could alread helped you

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.