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

Thread: Reverse the order of Strings in an ArrayList?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Reverse the order of Strings in an ArrayList?

    Hey guys. I'm trying to make a class called "purse" where I make an array list and store String in it (quarters, dimes, etc), and then I print out the names of each coin I stored in the purse. I got the printing out part just fine. Then I have to make a method where I reverse the order of the names and prints them out as well. I think I got this one right until I print them out and notice that I get an error in the output. This is the "reverse" method. Could anyone tell me what I'm doing wrong here?

    import java.util.ArrayList;
     
    public class Purse
    {
    	private ArrayList<String> coins;
    	private ArrayList<String> reverseArray;
     
    	public Purse()
    	{
    		ArrayList<String> coin = new ArrayList<String>();
    		coins = coin;
    	}
     
    	public void addCoin(String coinName)
    	{
    		coins.add(coinName);
    	}
     
    	public String reverse()
    	{
    		int n = coins.size();
    		for(int i = n; i == 0; i--)
    		{
    			String s = coins.get(n);
    			reverseArray.add(s);
    		}
    		String reverseWords = reverseArray.toString();
    		return reverseWords;
    	}
     
     
    	public String toString()
    	{
    		String words = coins.toString();
    		return words;
    	}
    }

    public class PurseTester 
    {
    	public static void main(String[] args)
    	{
    		Purse jean = new Purse();
    		jean.addCoin("Quarter");
    		jean.addCoin("Dime");
    		jean.addCoin("Nickel");
    		jean.addCoin("Dime");
     
    		System.out.println(jean.toString());
    		System.out.println(jean.reverse());
    	}
     
    }

    Output:
    [Quarter, Dime, Nickel, Dime]
    Exception in thread "main" java.lang.NullPointerException
    at Purse.reverse(Purse.java:27)
    at PurseTester.main(PurseTester.java:12)


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Reverse the order of Strings in an ArrayList?

    Which is line 27 of Purse.java? The runtime exception is saying that you are using a variable here that is null.

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reverse the order of Strings in an ArrayList?

    Quote Originally Posted by pbrockway2 View Post
    Which is line 27 of Purse.java? The runtime exception is saying that you are using a variable here that is null.
    String reverseWords = reverseArray.toString();

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Reverse the order of Strings in an ArrayList?

    OK. So reverseArray is null - that is it has not been assigned a value yet. And you cannot call a method like toString() on something that doesn't exist.

    You can test this claim (that reverseArray is null) easily enough:

    public String reverse()
    {
        int n = coins.size();
        for(int i = n; i == 0; i--)
        {
            String s = coins.get(n);
            reverseArray.add(s);
        }
        System.out.println("About to call toString() with reverseArray = " + reverseArray);
        String reverseWords = reverseArray.toString();
        return reverseWords;
    }

    If it turns out that reverseArray is null, go back to where you thought you had assigned it a value and figure out why that didn't happen. If you simply forgot to assign it any value, then do so and see what happens.

  5. #5
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reverse the order of Strings in an ArrayList?

    Quote Originally Posted by pbrockway2 View Post
    OK. So reverseArray is null - that is it has not been assigned a value yet. And you cannot call a method like toString() on something that doesn't exist.

    You can test this claim (that reverseArray is null) easily enough:

    public String reverse()
    {
        int n = coins.size();
        for(int i = n; i == 0; i--)
        {
            String s = coins.get(n);
            reverseArray.add(s);
        }
        System.out.println("About to call toString() with reverseArray = " + reverseArray);
        String reverseWords = reverseArray.toString();
        return reverseWords;
    }

    If it turns out that reverseArray is null, go back to where you thought you had assigned it a value and figure out why that didn't happen. If you simply forgot to assign it any value, then do so and see what happens.
    Ok, so I stated a value to the array and effectively it fixed the Exception. However, now when I print it I just get an empty array instead of the expected words in reverse order. What is wrong with my loop?

    [CODE]import java.util.ArrayList;

    public class Purse
    {
    private ArrayList<String> coins;
    private ArrayList<String> reverseArray;
    private String reverseWords;

    public Purse()
    {
    ArrayList<String> coin = new ArrayList<String>();
    coins = coin;
    ArrayList<String> reversed = new ArrayList<String>();
    reverseArray = reversed;
    }

    public void addCoin(String coinName)
    {
    coins.add(coinName);
    }

    public String reverse()
    {
    int n = coins.size();
    String reverseWords;

    for(int i = n; i == 0; i--)
    {
    String s = coins.get(n);
    reverseArray.add(s);
    }

    reverseWords = reverseArray.toString();
    return reverseWords;
    }


    public String toString()
    {
    String words = coins.toString();
    return words;
    }
    }
    [CODE]

  6. #6
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reverse the order of Strings in an ArrayList?

    	public String reverse()
    	{
    		int n = coins.size();
    		String reverseWords;
     
    		for(int i = n; i == 0; i--)
    		{
    			String s = coins.get(n);
    			reverseArray.add(s);
    		}
     
    		reverseWords = reverseArray.toString();
    		return  reverseWords;
    	}

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Reverse the order of Strings in an ArrayList?

    now when I print it I just get an empty array instead of the expected words in reverse order. What is wrong with my loop?
    System.out.println() - the poor man's debugger - to the rescue again...

    Have a look at what your loop is doing:

    System.out.println("About to start loop");
    for(int i = n; i == 0; i--)
    {
        System.out.println("In loop, i = " + i);
        String s = coins.get(n);
        reverseArray.add(s);
    }
    System.out.println("Loop finished");

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse the order of Strings in an ArrayList?

    How about just using Collections.reverse(list_parameters)? That saves a lot of your time
    to do or not to do is a question

Similar Threads

  1. How do I reverse an ArrayList ?
    By BostonBruins in forum Collections and Generics
    Replies: 1
    Last Post: September 15th, 2012, 04:34 PM
  2. Replies: 1
    Last Post: August 5th, 2012, 09:28 PM
  3. Strings not being added to arraylist
    By noel222 in forum What's Wrong With My Code?
    Replies: 30
    Last Post: February 10th, 2012, 05:53 PM
  4. How to sort an ArrayList of Strings?
    By noFear in forum Java Theory & Questions
    Replies: 6
    Last Post: September 15th, 2010, 03:23 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM