CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2013
    Posts
    46

    [RESOLVED] Scanner Class with the ArrayList

    Hello Java friends,

    I am new to joining a site like this so let me get straight to the point. I am trying to make an ArrayList that stores the users input of the ages of their grandchildren. I have this code started but am lost from there as I can not find enough information on the web on how to accomplish this task.

    Yes, I am an IT student with Java being my last class till I graduate, but this is not an actual assignment. I need to try to learn this task as i will be using a similar function in my project. The age of the grandchildren is something i made up as I seem to be missing how to store user inputs (specifically <Integers>), then to sum all the inputed ages (for right now I am just trying to show all the inputed ages as an array). I will be grateful for any and all help I receive.

    CODE:
    package grandchildren;

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

    public class GrandChildren
    {


    public static void main(String[] args)
    {
    Scanner in = new Scanner(System.in);

    //My ArrayList (i am getting an redundant data type here)
    ArrayList<Integer> childAge = new ArrayList<Integer>();

    //Asks for the ages of the grand children
    System.out.println("What are the ages of your grand children? ");
    int age = in.nextInt();
    if (age > 0)
    {
    childAge.add(age);
    }
    else
    {
    for(int i = 0; i <= childAge.size(); i++)
    {
    //I am getting an error here
    System.out.println(childAge[i]);
    }
    }
    }
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class with the ArrayList

    The variable: childAge is not an array. Using array notation ([]) is an error. To access elements in an object like an ArrayList, you need to use a method. Read the API doc for the ArrayList class to see what methods might be useful to get an item from an ArrayList.
    Norm

  3. #3
    Join Date
    Dec 2013
    Posts
    46

    Re: Scanner Class with the ArrayList

    I understand that I have to use the get() method but I am having difficulties implementing this. any help would be greatly appreciated.

    I have attached my modified code:
    Code:
    package grandchildren;
    
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class GrandChildren 
    {
    
       
        public static void main(String[] args) 
        {
            Scanner in = new Scanner(System.in);
            
            //My ArrayList
            ArrayList<Integer> childAge = new ArrayList<Integer>();
            
            //Asks for the ages of the grand children
            System.out.println("What are the ages of your grand children? ");
            // int age = in.nextInt();
            while(in.hasNextInt())
            {
                childAge.add(in.nextInt());
            }
            
            for(int childAge2 : childAge)   //I have tried 2 different ways
            {
                System.out.println(childAge2);
            }
            for (int i = 0; i < childAge.size(); i++)
            {
                int age = childAge.get(i);
                System.out.println(age);
            }
         
            
        }
    }
    Last edited by BigDadd215; December 17th, 2013 at 08:35 AM.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class with the ArrayList

    I am having difficulties
    Please explain. If there are error messages, copy the fill text and paste it here.


    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    Norm

  5. #5
    Join Date
    Dec 2013
    Posts
    46

    Re: Scanner Class with the ArrayList

    I am not getting an error this time....... I can not get it to print the inputed ArrayList. If I input a series of integers (1 2 3 for example), it does not print out 1 2 3.

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class with the ArrayList

    Can you copy the contents of the window from when you execute the program and paste it here to show what you are talking about.
    The code works for me. I get the following output:
    1
    2
    3
    1
    2
    3
    Norm

  7. #7
    Join Date
    Dec 2013
    Posts
    46

    Re: Scanner Class with the ArrayList

    This is what I get in my console window:
    run:
    What are the ages of your grand children?
    1 2 3
    BUILD SUCCESSFUL (total time: 12 seconds)

  8. #8
    Join Date
    Dec 2013
    Posts
    46

    Re: Scanner Class with the ArrayList

    The 1 2 3 is what I put in, but I don't receive any output.

  9. #9
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class with the ArrayList

    The Scanner class's method is waiting for more input. You need a way to tell it that the input is finished.

    One possible solution: End the input with a non-digit like this: 1 2 3 X
    Last edited by Norm; December 17th, 2013 at 10:20 AM.
    Norm

  10. #10
    Join Date
    Dec 2013
    Posts
    46

    Re: Scanner Class with the ArrayList

    What would be an acceptable way to terminate or exit the scanner? Given this section of code
    Code:
        while(in.hasNextInt())
                {
                   childAge.add(in.nextInt());
                }
            
                for (int element : childAge)
                {
                    System.out.print(element);
                }
    I am trying to either use "X" or 0 as the exit qualifier.

  11. #11
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class with the ArrayList

    Any non-numeric value which will cause the hasNextInt() method to return false will end the while() loop. When you ask the user for his input, you need to tell him how to end it. It's up to you how to tell him what to enter.
    Norm

  12. #12
    Join Date
    Dec 2013
    Posts
    46

    Re: Scanner Class with the ArrayList

    I have it figured out... Thanks for everything and all the advise.

    Code:
    package grandchildren;
    
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class GrandChildren 
    {
    
       
        public static void main(String[] args) 
        {
            Scanner in = new Scanner(System.in);
            
           
            ArrayList<Integer> childAge = new ArrayList<Integer>();
            int age = 0;
           
            System.out.println("What are the ages of your grand children? (Enter any letter value to exit)");
           
                while(in.hasNextInt())
                {
                    age = in.nextInt();
                    if (age >= 1)
                            {
                                childAge.add(age);
                            }
                    else{System.out.println("That is an invalid input.");}
                }
            System.out.println(childAge);

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
  •  





Click Here to Expand Forum to Full Width

Featured