6 Replies Latest reply: Feb 3, 2007 3:53 AM by 807599 RSS

    ArrayList / Scanner Issues...

    807599
      Hello everyone,

      I'm in an AP Computer Science class in high school and I've run into a problem using ArrayLists. This project is just an experiment, not an actual assignment

      The purpose of this project is to design a BASIC database with as many fields as the user desires. (Example: CD COLLECTION containing the fields "album," "artist," and "album length")

      The problem I am having occurs when I try to assign names to each ArrayList. Here is my coding so far:
      import java.util.ArrayList;
      import java.util.Scanner;
      public class EXPERIMENT
      {
          public static void main(String[] args)
          {
              Scanner input = new Scanner(System.in);
              
              System.out.println("CD COLLECTION EXPERIMENT");      
              
              System.out.print("Enter Number of Fields: ");
              int totalFields = input.nextInt();
              
              for (int i = 1; i <= totalFields; i++)
              {
                  System.out.println("Enter First Field Name: ");
                  char fieldName = input.nextLine();
                  ArrayList<Char> value of fieldName = new ArrayList<Char>();
              }
          }
      }
        • 1. Re: ArrayList / Scanner Issues...
          doremifasollatido
          Common question--third time I've seen it today (always worded slightly differently).

          Use a Map:
          http://forum.java.sun.com/thread.jspa?threadID=5131570

          (The left side of the declaration of the "map" variable could be a Map, instead of a HashMap. The right needs to be a HashMap. For your code, you'll probably want a "Map < String , String >".)
          • 2. Re: ArrayList / Scanner Issues...
            807599
            You're trying to name the arraylist with the value in the fieldName. You cannot do that. This is because the left side of a declaration should be a variable not a value. You might want to try and use a Map.
            • 3. Re: ArrayList / Scanner Issues...
              807599
              Why an ArrayList<Char>? A list of characters? What for?
              • 4. Re: ArrayList / Scanner Issues...
                807599
                Please read all the excellent feedback then note:
                A char and Character are only single characters.
                When you read a line with:
                input.nextLine();
                It returns a String which is a Special Object for handling the common case of many char's in an array of char.

                So change your char to java.lang.String or just String since you automagically
                get : import java.lang.*;

                Read up on java.lang.String in the JavaDoc it is a very powerful tool that Java provides.

                Good luck and keep asking questions, its how we learn.
                • 5. Re: ArrayList / Scanner Issues...
                  807599
                  Also "Char" means nothing. Please remember that Java is case sensitive.
                  If you want the Object version of the primitive char then you need to use Character
                  • 6. Re: ArrayList / Scanner Issues...
                    807599
                    one last thing....
                    When in doubt about a program in java and you are not sure why something
                    does not work or in this case it should not compile....
                    Carefully examine the compiler output.
                    Its there to help you resolve these issues.