0

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...

2
  • 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. Commented Feb 22, 2014 at 15:07
  • use equals instead of "==" while your compare two Strings Commented Feb 22, 2014 at 15:08

5 Answers 5

4

You are looping forever (even after correcting the == error) because you always check the same tweet. This would probably work better:

List<String> tweets = new ArrayList<String>();

while (true) {
    String tweet = twitterInput.next();
    if ("done".equals(tweet)) break;
    tweets.add(tweet);
}
Sign up to request clarification or add additional context in comments.

1 Comment

yeah, he's adding the same string forever, so heap space is depleted
1

You are never setting go to true because the String comparison never succeeds. Don't compare strings with ==. Use the equals() method instead. So change:

if(tweet == "done"){

to:

if(tweet.equals("done")){

However, this won't solve your problem entirely. You also need to update the tweet variable inside of the loop, otherwise you'll always be comparing against the same String. See assylias' answer for a code example.

1 Comment

@MartinDinov if tweet is not "done" when entering the loop, it will be an infinite loop (even after your correction).
1
        while(go == true){
            tweets.add(tweet);
            if(tweet.equals("done")) { // this line should be changed
                go = false;
            }
        }

In your case, tweet == "done" is never going to execute, and hence the while loop gets to infinite loop. This causes Null Pointer Exception.

Comments

1

The problem is, that you first read the tweet and then initiate a while cycle, that adds the same tweet over and over again, until you run out of memory. Add

System.out.println(tweets.size());

behind

tweets.add(tweet);

to get a better grasp of what's happening.

Comments

0

There are several items you should take from this learning experience:

  1. There is a difference between comparing objects (to see if they are the same object) and comparing the String value of objects. For string comparison, you will usually want to use the form String1.equalsIgnoreCase(String2) (If case matters, then one uses String1.equals(String2).)
  2. For loops that involve getting input from somewhere you may want to use the form

    line = [get input]
    while (!line.equealsIgnoreCase([end string]) {
        [ do work on line ]
        line = [get input]
    }
    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.