Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Creating a scanner and putting input into arrays.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a scanner and putting input into arrays.

    I'm trying to read user input from the terminal and separate the input into separate arrays depending on if the user input is an integer, scanner, or a string. The terminal should keep asking the user for input until the user types "quit". Please help!

    import java.util.*;
     
    public class arrayScanner
    {
        public static void main(String[] args)
        {
    	ArrayList<Integer> intList = new ArrayList<Integer>();
    	ArrayList<Double> doubleList = new ArrayList<Double>();
    	ArrayList<String> otherList = new ArrayList<String>();
     
            Scanner scanner = new Scanner(System.in);
    	System.out.println("Enter an int, double, any random text, or type \"quit\" to end:" );
    	String input = scanner.nextLine();
    	if(scanner.nextLine() == "quit")
    	{
                System.out.println("Integers: \n");
    	    for(int i = 0; i < intList.size(); i++)
    	    {
                    System.out.println("Integer list is" + intList.get(i));
    	    }
    	    System.out.println("Doubles: \n");
    	    for(int i = 0; i < doubleList.size(); i++)
    	    {
                    System.out.println("Double list is" + doubleList.get(i));
    	    }
    	    System.out.println("Other: \n");
    	    for(int i = 0; i < otherList.size(); i++)
    	    {
                    System.out.println("Other list is" + otherList.get(i));
    	    }
    	}
    	else if(scanner.hasNextInt())
    	{   
                intList.add(input);
    	}
    	else if(scanner.hasNextDouble())
    	{
                doubleList.add(input);
    	}
    	else if(scanner.hasNext())
    	{
                otherList.add(input);
    	}
     
        }
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Creating a scanner and putting input into arrays.

    Please help!
    I and others can see what the problems with you code. but please give a specific questions. please specify what the problem is.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a scanner and putting input into arrays.

    for one, my compiler says "error: no suitable method found for add(String)" for intList.add(input); and doubleList.add(input);

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Creating a scanner and putting input into arrays.

    it is because the list intList was generic into Integer object only. therefore, it will accept Integer object or int primitive type (because of auto boxing I guess). and what you did in this statement of your code intList.add(input); is adding an element of Object String, your input variable is an Object reference variable for String object.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a scanner and putting input into arrays.

    oh. I thought I checked if it was an int with "else if(scanner.hasNextInt())" how would I use my scanner to determine the type of the user input?

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Creating a scanner and putting input into arrays.

    oh. I thought I checked if it was an int with "else if(scanner.hasNextInt())"
    yes, you checked it, but the problem is when adding an element to your intList, input is String. try to parse it in int. or other method
    intList.add(input); - that was the problem, adding an element of Object String but ArrayList was generic to Integer.

Similar Threads

  1. [SOLVED] Parsing input from a file using Scanner
    By Helplessdrowningpuppy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2014, 04:15 PM
  2. Scanner multipule input [Solved]
    By Dragon-RD in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 17th, 2013, 09:03 AM
  3. File input using Scanner help.
    By AaronHarris in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 4th, 2013, 07:15 AM
  4. Scanner skipping input
    By hblakek in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 21st, 2012, 01:44 PM
  5. [SOLVED] Faster Run Time - Scanner(file) and Arrays
    By Dogeatdog6 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 7th, 2011, 09:25 AM