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?
Code :
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;
}
}
Code :
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:
Quote:
[Quarter, Dime, Nickel, Dime]
Exception in thread "main" java.lang.NullPointerException
at Purse.reverse(Purse.java:27)
at PurseTester.main(PurseTester.java:12)
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.
Re: Reverse the order of Strings in an ArrayList?
Quote:
Originally Posted by
pbrockway2
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();
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:
Code :
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.
Re: Reverse the order of Strings in an ArrayList?
Quote:
Originally Posted by
pbrockway2
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:
Code :
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]
Re: Reverse the order of Strings in an ArrayList?
Code :
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;
}
Re: Reverse the order of Strings in an ArrayList?
Quote:
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:
Code :
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");
Re: Reverse the order of Strings in an ArrayList?
How about just using Collections.reverse(list_parameters)? That saves a lot of your time