4 Replies - 326 Views - Last Post: 26 December 2012 - 11:29 AM Rate Topic: -----

#1 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

Scanning in line of integers into arraylist (or array)

Posted 26 December 2012 - 09:33 AM

What I'm trying to do is read a line of 4 integers from the standard input. Integers will be entered like so: 100 20 30 20.

I've tried to do this using a normal array, but it did not work. I would enter the four integers, but nothing would happen when I pressed enter. So I tried using the arraylist class as so:

import java.util.ArrayList;
import java.util.Scanner;


public class Internship {
	public static void main(String[] args){
		//int[] lotteryInput = new int[4];
		ArrayList<Integer> lotteryInput = new ArrayList<Integer>(4);
		Scanner scan = new Scanner(System.in);
		for(int i = 0; i < lotteryInput.size() ; i++){
			while(scan.hasNextInt()){
				lotteryInput.set(i, scan.nextInt());
			}
			
			
		}
		
	
	}

}



This does not work. The scanner isn't even scanning for any thing. Here's my regular array code:

import java.util.ArrayList;
import java.util.Scanner;


public class Internship {
	public static void main(String[] args){
		//int[] lotteryInput = new int[4];
		int[] lotteryInput = new int[4];
		Scanner scan = new Scanner(System.in);
		for(int i = 0; i < lotteryInput.length ; i++){
			while(scan.hasNextInt()){
				lotteryInput[i] = scan.nextInt();
			}
			
			
		}
		System.out.println(lotteryInput);
		
	
	}

}



Any help would be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Scanning in line of integers into arraylist (or array)

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: Scanning in line of integers into arraylist (or array)

Posted 26 December 2012 - 09:45 AM

You're treating an ArrayList like an array, and you should take advantage of the additional capabilities ArrayList has to offer. The following will work, but it can be optimized:
import java.util.ArrayList;
import java.util.Scanner;


public class TestClass2
{
    public static void main(String[] args)
    {
        //int[] lotteryInput = new int[4];
        ArrayList<Integer> lotteryInput = new ArrayList<Integer>();
        Scanner scan = new Scanner(System.in);

        System.out.print( "Enter 4 integers separated by white space: " );

        for ( int i = 0 ; i < 4 ; i++ )
        {
            lotteryInput.add( scan.nextInt() );
        }
        
        scan.close();
        
        System.out.println( lotteryInput );
    }
}

Here's an example run:
Enter 4 integers separated by white space: 40 30 20 10
[40, 30, 20, 10]


Was This Post Helpful? 1
  • +
  • -

#3 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

Re: Scanning in line of integers into arraylist (or array)

Posted 26 December 2012 - 09:54 AM

Thank you very much. That works. I think the close() method is what closes the scanning correct?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: Scanning in line of integers into arraylist (or array)

Posted 26 December 2012 - 09:58 AM

Leaving the Scanner object "open" is unfinished business. However, closing it prevents it being used again. From the Scanner API page:

Quote

public void close()
Closes this scanner.
If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.

Attempting to perform search operations after a scanner has been closed will result in an IllegalStateException.

Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,822
  • Joined: 20-September 08

Re: Scanning in line of integers into arraylist (or array)

Posted 26 December 2012 - 11:29 AM

Quote

 for(int i = 0; i < lotteryInput.size() ; i++){

That won't execute (and therefore the inner loop won't either) as the size of 'lotteryInput' is zero at that point, for obvious reasons. size() != capacity()
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1