I got this error message when trying to run a school project. If it's helpful I need to write a code that takes in strings from the user and counts the amount of #'s they enter.
Here is my project code:
package edu.bsu.cs121.albeaver;
import java.util.*;
public class HashTagCounter {
public static void main(String args[]){
boolean go = true;
System.out.println("Please tweet your tweets: ");
Scanner twitterInput = new Scanner(System.in);
String tweet = twitterInput.next();
ArrayList<String> tweets = new ArrayList<String>();
while(go == true){
tweets.add(tweet);
if(tweet == "done"){
go = false;
}
}
System.out.println(tweets);
twitterInput.close();
}
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at edu.bsu.cs121.albeaver.HashTagCounter.main(HashTagCounter.java:16)
I'm not sure what to do...
tweet == "done"is never true, because in Java,==compares strings by their reference, not their value. Also, you have to read the next tweet within the loop.