[英]Out of Memory exception java
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... 我不确定该怎么办...
You are looping forever (even after correcting the ==
error) because you always check the same tweet. 您将永远循环(即使在更正
==
错误之后也是如此),因为您始终检查同一条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);
}
You are never setting go
to true
because the String
comparison never succeeds. 你永远设置
go
到true
,因为String
比较从未成功。 Don't compare strings with ==
. 不要将字符串与
==
进行比较。 Use the equals()
method instead . 请改用
equals()
方法 。 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
. 您还需要在循环内部更新
tweet
变量,否则,您将始终与同一String
进行比较。 See assylias' answer for a code example. 有关代码示例,请参见assylias的答案。
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. 在您的情况下,
tweet == "done"
永远不会执行,因此while循环进入无限循环。 This causes Null Pointer Exception
. 这将导致
Null Pointer Exception
。
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. 问题是,您首先阅读该tweet,然后启动一个while周期,一次又一次地添加相同的tweet,直到内存用完。 Add
加
System.out.println(tweets.size());
behind 背后
tweets.add(tweet);
to get a better grasp of what's happening. 以便更好地了解正在发生的事情。
There are several items you should take from this learning experience: 您应该从此学习经历中选择几个项目:
String1.equalsIgnoreCase(String2)
(If case matters, then one uses String1.equals(String2).) String1.equalsIgnoreCase(String2)
的形式(如果大小写有关,则使用String1.equals(String2)。) 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] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.