Results 1 to 15 of 15
Thread: ArrayList Help
- 10-28-2012, 06:22 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 20
- Rep Power
- 0
ArrayList Help
Im am taking one of my last classes for my Bachelors Degree in Computer Info Systems. My weak point is programming and unfortunately for me Java happened to be in my last semester. Im stuck on a programming homework assignment and I can not figure it out.
Basically the point of this is to allow the user to input a number that is the length of the ArrayList. Then the program will ask the user for names until that length for ArrayList is reached. At which point it prints the size of the ArrayList, the names, and position in the list. As of right now I am getting an error code. Id appreciate any help I can get.
Here is my coding:
Java Code://ArrayListMod.java by Tyler 10/27/2012 import java.util.*; public class ArrayListMod { public static void main(String[] args) { int numNames; int[] names; Date today = new Date(); System.out.println("ArrayListMod.java" + "\nby Tyler " + today); Scanner input = new Scanner(System.in); //Enter number of names to have in array System.out.println("Enter number of names to input: "); numNames = input.nextInt(); //Create array to hold number of names to enter names = new int[numNames]; ArrayList<String>namesList = new ArrayList<String>(); for (int x = 0; x < names.length; x++) { System.out.print("Enter name " + (x + 1) + ": "); names[x] = input.nextInt(); namesList.add(input); } display(namesList); } public static void display(ArrayList namesList) { System.out.println("\nThe size of the list is " + namesList.size()); for(int x = 0; x < namesList.size(); ++x) System.out.println("Position " + x + " Name: " + namesList.get(x)); } }Java Code:Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Tyler>cd C:\Users\Tyler\Documents\School Work\CIS280\Project 5 C:\Users\Tyler\Documents\School Work\CIS280\Project 5>javac ArrayListMod.java ArrayListMod.java:29: cannot find symbol symbol : method add(java.util.Scanner) location: class java.util.ArrayList<java.lang.String> namesList.add(input); ^ 1 error C:\Users\Tyler\Documents\School Work\CIS280\Project 5>
- 10-28-2012, 06:25 PM #2
Re: ArrayList Help
You're trying to add the Scanner to the ArrayList, while the ArrayList is set to only accept Strings.
Java Code:Scanner input = new Scanner(System.in); ArrayList<String>namesList = new ArrayList<String>(); namesList.add(input);
- 10-28-2012, 06:41 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 20
- Rep Power
- 0
- 10-28-2012, 06:54 PM #4
Re: ArrayList Help
Scanner only helps you get the strings, so you wouldn't convert a Scanner to a String, no. I haven't worked much with Scanner though, so I can't give any example on how exactly you should go about getting the String, but reading the Javadoc for Scanner seems to suggest Scanner.nextLine(). I'm sure someone better versed with the inner workings of Scanner can help you with that part though.
- 10-28-2012, 11:46 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 20
- Rep Power
- 0
- 10-29-2012, 12:24 AM #6
- 10-29-2012, 12:32 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 20
- Rep Power
- 0
- 10-29-2012, 12:37 AM #8
Re: ArrayList Help
No it isn't!
Originally your tried inserting a Scanner reference into a String List. Then after some advice you said you tried nextLine and it didn't work. So therefore you must have changed your code and got another error. What is the changed code and what was the error? We don't read minds.
- 10-29-2012, 12:41 AM #9
Member
- Join Date
- Oct 2012
- Posts
- 20
- Rep Power
- 0
- 10-29-2012, 12:43 AM #10
Re: ArrayList Help
:headdesk:
Yes it is the correct option. Just because you got another error when you tried it doesn't mean it was wrong and you should go back to your orignal code. Post the changed code and the new error message here so someone can help you.
- 10-29-2012, 01:03 AM #11
Member
- Join Date
- Oct 2012
- Posts
- 20
- Rep Power
- 0
Re: ArrayList Help
New code is as follows. Notice line 28 is pretty much the only change aside from adding documentation.
New Error code:Java Code://ArrayListMod.java by Tyler 10/27/2012 import java.util.*; public class ArrayListMod { public static void main(String[] args) { int numNames; int[] names; Date today = new Date(); System.out.println("ArrayListMod.java" + "\nby Tyler " + today); Scanner input = new Scanner(System.in); //Enter number of names to have in array System.out.println("Enter number of names to input: "); numNames = input.nextInt(); //Create array to hold number of names to enter names = new int[numNames]; //Defined ArrayList ArrayList<String>namesList = new ArrayList<String>(); //Enter names until x is greater than number entered for array length for (int x = 0; x < names.length; x++) { System.out.print("Enter name " + (x + 1) + ": "); names[x] = input.nextLine(); namesList.add(input); } //Calls display method. display(namesList); } public static void display(ArrayList namesList) { System.out.println("\nThe size of the list is " + namesList.size()); for(int x = 0; x < namesList.size(); ++x) System.out.println("Position " + x + " Name: " + namesList.get(x)); } }
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Tyler\Documents\School Work\CIS280\Project 5>javac ArrayListMod.java
ArrayListMod.java:28: incompatible types
found : java.lang.String
required: int
names[x] = input.nextLine();
^
ArrayListMod.java:29: cannot find symbol
symbol : method add(java.util.Scanner)
location: class java.util.ArrayList<java.lang.String>
namesList.add(input);
^
2 errors
C:\Users\Tyler\Documents\School Work\CIS280\Project 5>
- 10-29-2012, 01:10 AM #12
Re: ArrayList Help
Read the error messages. They are there for a reason.
First one: the nextLine method returns a String and you are trying to insert it into a int array. just like the messages says. Found: String, required: int
Second one is the same as before you cannot insert the Scanner into your String ArrayList, you must insert a String which is obtained from........
- 10-29-2012, 01:41 AM #13
Member
- Join Date
- Oct 2012
- Posts
- 20
- Rep Power
- 0
Re: ArrayList Help
Have no idea what you said, but I some how figured it out. I added another scanner to get names and changed the logic a little bit. Here is the final code:
Java Code://ArrayListMod.java by Tyler 10/27/2012 import java.util.*; public class ArrayListMod { public static void main(String[] args) { int numNames; int[] names; String name; Date today = new Date(); System.out.println("ArrayListMod.java" + "\nby Tyler " + today); Scanner input = new Scanner(System.in); Scanner nameEntered = new Scanner(System.in); //Enter number of names to have in array System.out.println("Enter number of names to input: "); numNames = input.nextInt(); //Create array to hold number of names to enter names = new int[numNames]; //Defined ArrayList ArrayList<String>namesList = new ArrayList<String>(); //Enter names until x is greater than number entered for array length for (int x = 0; x < names.length; x++) { System.out.print("Enter name " + (x + 1) + ": "); name = nameEntered.nextLine(); namesList.add(name); } //Calls display method. display(namesList); } public static void display(ArrayList namesList) { System.out.println("\nThe size of the list is " + namesList.size()); for(int x = 0; x < namesList.size(); ++x) System.out.println("Position " + x + " Name: " + namesList.get(x)); } }
- 10-29-2012, 01:47 AM #14
Re: ArrayList Help
You could have used a single Scanner you just needed to call the correct methods which you weren't. But for some bizarre reason once you created another Scanner object you managed to call the correct methods. :shrugs:
- 10-29-2012, 01:51 AM #15
Member
- Join Date
- Oct 2012
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
copying contents of an ArrayList to another ArrayList
By ankit1801 in forum New To JavaReplies: 8Last Post: 03-27-2011, 06:07 AM -
sorting arraylist based on another arraylist
By busdude in forum New To JavaReplies: 4Last Post: 02-07-2011, 11:48 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks