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 18 of 18

Thread: Array Lists help!!

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Lists help!!

    Hello everybody!

    I am new to java and i have a problem. I need to create an arraylist from the coordinates οφ the real and estimated values that I have read from a file.But i can't seem to do that! Can you help me please??


    I woul really appreciate your help cause i m lost!!!
    Last edited by lilika; January 6th, 2011 at 02:06 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    sCurrentLine = s.readLine()) != null

    First off,

    sCurrentLine ==s.readLine()

    while ((sCurrentLine = s.readLine()) != null) {

    This line won't work.

    while((sCurrentLIne == s.readLine()) && (s.readLine() !=null)

    might work.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    mat = pat.matcher(sCurrentLine);

    sCurrentLine hasn't been initialized yet.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    // Print line as read from file
    System.out.println(sCurrentLine)

    Well, to just print out the lines alone read from the file without doing anything to them,

    well....you'd need to define the FileReader first before you do. But after that

    while (inFile.hasNext())
    {
    sCurrentLine = inFile.nextLine();
    System.out.prinltn(sCurrentLine);
    }

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    int n = myArray.size();
    for (int i = 0; i < n; i++) {
    System.out.println(myArray.get(i));
    }

    You never added anything.

    Your size will be 0.

    You have to

    myArray.add(int index, Integer data);

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists help!!

    Quote Originally Posted by javapenguin View Post
    // Print line as read from file
    System.out.println(sCurrentLine)

    Well, to just print out the lines alone read from the file without doing anything to them,

    well....you'd need to define the FileReader first before you do. But after that

    while (inFile.hasNext())
    {
    sCurrentLine = inFile.nextLine();
    System.out.prinltn(sCurrentLine);
    }

    Yes i knew that but the problem was that i wanted to separate the numbers from the text and the symbols. That's where i messed up. I managed to do the first thing (read and seperate the integers) but i can't understand how to create the array list from the code i have...

    thank you for your answers but could you be a little more specific cause i m really new to java.

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists help!!

    Quote Originally Posted by javapenguin View Post
    int n = myArray.size();
    for (int i = 0; i < n; i++) {
    System.out.println(myArray.get(i));
    }

    You never added anything.

    Your size will be 0.

    You have to

    myArray.add(int index, Integer data);
    You are absolutely right but i dont know how to fill: myArray.add(int index, Integer data); with the integers i found when i read the file. Whta is my index and what is my data?

    Sorry for the stupid questions :-(

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    Well, to separate the numbers and the text and symbols, maybe you could use a StringTokenizer.
    StringTokenizer (Java 2 Platform SE v1.4.2)

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    Quote Originally Posted by lilika View Post
    You are absolutely right but i dont know how to fill: myArray.add(int index, Integer data); with the integers i found when i read the file. Whta is my index and what is my data?

    Sorry for the stupid questions :-(
    For starters, what exactly are you trying to add?

    Each number individually, each number as a pair, each number as a pair with reference to another pair?

    To add it, do something perhaps like this:

    StringTokenizer tokenizer = new StringTokenizer(sCurrentLine);

    String temp = tokenizer.nextToken();

    if (temp.equals("("))
    ignore Token and go on

    else if (temp.equals(")"))
    ignore Token and go on

    else if (temp.equals("->>>>>noise->>>>"))
    ignore Token and go on

    else if (temp.equals(","))
    ignore Token and go on

    else
    {

    Integer temp2 = Integer.parseInt(token);

    myArray.add(temp2);

    }

    Also, I'm sorry earlier. There are two possible add methods for ArrayList.

    add(E data) adds the data to the end of your ArrayList

    add(int index, E data) adds the data at the index index of your ArrayList.
    Last edited by javapenguin; January 3rd, 2011 at 03:58 PM.

  10. #10
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists help!!

    Quote Originally Posted by javapenguin View Post
    For starters, what exactly are you trying to add?

    Each number individually, each number as a pair, each number as a pair with reference to another pair?

    To add it, do something perhaps like this:

    StringTokenizer tokenizer = new StringTokenizer(sCurrentLine);

    String temp = tokenizer.nextToken();

    if (temp.equals("("))
    ignore Token and go on

    else if (temp.equals(")"))
    ignore Token and go on

    else if (temp.equals("->>>>>noise->>>>"))
    ignore Token and go on

    else if (temp.equals(","))
    ignore Token and go on

    else
    {

    Integer temp2 = Integer.parseInt(token);

    myArray.add(temp2);

    }

    Also, I'm sorry earlier. There are two possible add methods for ArrayList.

    add(E data) adds the data to the end of your ArrayList

    add(int index, E data) adds the data at the index index of your ArrayList.
    Thank you so much! I will try that and tell you about it! I am trying to add each number individually to an array list.I hope it will work!

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Array Lists help!!

    Quote Originally Posted by javapenguin
    while ((sCurrentLine = s.readLine()) != null) {

    This line won't work.
    Yes, it will work..

    To add values to an arraylist, just call the add function, not index is necessary when appending to the List

  12. #12
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists help!!

    I wrote it this way I don't know if I did it right, but there are errors on the lines with "ignore Token and go on;"

    Am I missing something???

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
     
    public class Main {
     private static String token;
     
        public static void main(String[] args) throws IOException {
     
     String sCurrentLine;
    ArrayList<Integer> myArray = new ArrayList<Integer>();
     BufferedReader s = null;
     s = new BufferedReader(new FileReader("measurements.txt"));
     
    while ((sCurrentLine = s.readLine()) != null) {
     
    StringTokenizer tokenizer = new StringTokenizer(sCurrentLine);
     
    String temp = tokenizer.nextToken();
     
    if (temp.equals("(")){
    ignore Token and go on;
     
    } else if (temp.equals(")")){
    ignore Token and go on;
     
    }else if (temp.equals("->>>>>noise->>>>")){
    ignore Token and go on;
     
        }
        else{
     
                    }
       if (temp.equals(",")) {
    ignore Token and go on;
     
        }
        else
    {
     
    Integer temp2 = Integer.parseInt(token);
     
    myArray.add(temp2);
     
    }
            }
        }
        }

    The output is:

    Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:417)
    at java.lang.Integer.parseInt(Integer.java:499)
    at xristina.Main.main(Main.java:45)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  13. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    sCurrentLIne = s.readLine();

  14. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
     
    public class Main {
     private static String token;
     
        public static void main(String[] args) throws IOException {
     
     String sCurrentLine;
    ArrayList<Integer> myArray = new ArrayList<Integer>();
     BufferedReader s = null;
     s = new BufferedReader(new FileReader("measurements.txt"));
     
    while ((sCurrentLine = s.readLine()) != null) {
    sCurrentLine = s.readLine(); // you were missing this part
     
    StringTokenizer tokenizer = new StringTokenizer(sCurrentLine);
     
    String temp = tokenizer.nextToken();
     
    if (temp.equals("(")){
    ignore Token and go on;
     
    } else if (temp.equals(")")){
    ignore Token and go on;
     
    }else if (temp.equals("->>>>>noise->>>>")){
    ignore Token and go on;
     
        }
        else{
     
                    }
       if (temp.equals(",")) {
    ignore Token and go on;
     
        }
        else
    {
     
    Integer temp2 = Integer.parseInt(token);
     
    myArray.add(temp2);
     
    }
            }
        }
        }

  15. #15
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    Also, you have an empty else.

  16. #16
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists help!!

    Yes i saw that and corrected it! It s late at night here!Sorry for that! But still the same errors in the "ignore Token and go on;" lines..

    package xristina;
     
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
     
    public class Main {
     private static String token;
     
        public static void main(String[] args) throws IOException {
     
     String sCurrentLine;
    ArrayList<Integer> myArray = new ArrayList<Integer>();
     BufferedReader s = null;
     s = new BufferedReader(new FileReader("measurements.txt"));
     
    while ((sCurrentLine = s.readLine()) != null) {
    sCurrentLine = s.readLine(); // you were missing this part
     
    StringTokenizer tokenizer = new StringTokenizer(sCurrentLine);
     
    String temp = tokenizer.nextToken();
     
    if (temp.equals("(")){
    ignore Token and go on;
     
    } else if (temp.equals(")")){
    ignore Token and go on;
     
    }else if (temp.equals("->>>>>noise->>>>")){
    ignore Token and go on;
     
        }
        else{
     
     
       if (temp.equals(",")) {
    ignore Token and go on;
     
        }
        else
    {
     
    Integer temp2 = Integer.parseInt(token);
     
    myArray.add(temp2);
     
     }
     }
     }
     }
     }

    I don't mean to tire you. You are really helpfull. I must be doing something wrong but I m sure I ll Find it!

  17. #17
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Array Lists help!!

    What I meant by the psuedocode "Ignore token and go on" was to simply just do nothing with the token. Just add the ones that are ints.
    String temp;
    // I just fixed it to define temp before the while loop.  Otherwise, it'll be null and won't be defined till it hits the while loop, which it won't since it'll still be null.
     
    while (temp !=null)
    {
     temp = tokenizer.nextToken();
     
    if (temp.equals("(")){
    // don't add it.  Just go on
    }
     
    } else if (temp.equals(")")){
    // don't add it. Just go on.  
     
    }else if (temp.equals("->>>>>noise->>>>")){
    // don't add it. Just go on.
     
        }
        else{
     
     
       if (temp.equals(",")) {
    // don't add it. Just go on.
     
        }
        else
    {
     
    Integer temp2 = Integer.parseInt(temp);
     
    myArray.add(temp2);
     
     }
    }
    }
    Last edited by javapenguin; January 3rd, 2011 at 05:51 PM.

  18. #18
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists help!!

    Thank you so much. I managed to do that. Now that I made the array list, i have to loop throught it in order to calculate some averages. But i don't want the average of all the array list but from some specific numbers( which are in specific places)

    ex. A B C D E F G H I J K L ....

    I need the average of C+G+K+....+/ how many are they.
    Last edited by lilika; January 4th, 2011 at 11:00 AM.

Similar Threads

  1. Operations with lists
    By datreta in forum Collections and Generics
    Replies: 8
    Last Post: October 29th, 2010, 08:54 AM
  2. Linked Lists
    By Jnoobs in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2010, 04:09 PM
  3. Array Lists, PLEASE HALP
    By Iamthecheese in forum Threads
    Replies: 10
    Last Post: September 7th, 2010, 08:43 PM
  4. Problems in linked lists
    By Hotzero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2010, 09:25 AM
  5. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM