所以,我正在尝试为即将到来的决赛练习,我只是为了娱乐和学习而进行随机编码。今晚我偶然发现了我以前从未见过的错误代码。我的代码似乎没有进入for循环,然后弹出错误消息。有任何想法吗?谢谢!
Please enter a sentence
Testing the program
You entered :
testing the program
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:421)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at FinalStudy.main(FinalStudy.java:22)
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Please enter a sentence");
String sentence=input.nextLine();
sentence=sentence.toLowerCase();
System.out.println("You entered : \n" + sentence);
//Doesn't seem to make it to this loop because it only prints the initial sentence
for (int i=0; i<sentence.length(); i++)
{
if (sentence.charAt(i)=='a' || sentence.charAt(i)=='e' || sentence.charAt(i)=='i')
{
sentence=sentence.substring(0,i) + "*" + sentence.substring(0,(i+1));
}
}
System.out.println("This is your new sentence: \n" + sentence);
}
答案 0 :(得分:4)
TLDR:你在这里有一个无限循环,它会扩展String'句子',直到你的内存不足为止。但我不会告诉你如何解决你的问题。
此外,您需要学习如何在程序中使用断点和打印语句进行调试。一个简单的谷歌搜索就足够了。
解释为什么它不起作用:
Imagine that I input the sentence "abc" and let's go through the for loop.
First, i = 0, and the length of 'sentence' = 3.
sentence.charAt(0) == 'a' is true
thus 'sentence' is now set to be the substring from 0 up to 0 (nothing),
plus '*', plus the substring from 0, up to 1 ("a").
Now the String 'sentence' is set to "*a".
i = 1, length = 2,
sentence.charAt(1) == 'a' is true
'sentence' is now set to be the substring from 0 up to 1 ("*"),
plus '*', plus the substring from 0 up to 2 ("*a");
Now the String 'sentence' is set to ***a
The for loop continues in this manner, doubling the length of the string
'sentence' whenever the variable i reaches sentence.length()-1. Eventually the
computer runs out of memory because it cannot store an infinite length string
and the program crashes.
一些提示: 当您单击IDE中的行号时,会导致出现“断点”。这意味着如果您以“调试模式”运行程序,解释器将在该行暂停并允许您查看某些信息,例如某些变量的值。 调试时的另一个技巧是使用print语句显示有关代码中发生的事情的信息,通常以这种方式显示变量。
PS每个人都曾经这样问过问题,所以不要气馁,因为你无法弄清问题。将来使用上面的建议尝试先调试,然后尝试解决方案的其他来源。通常,像这样的简单错误是问题的原因,可以通过调试找到它们。答案 1 :(得分:2)
使用替换方法
Socket socket = serverSocket.accept();
InputStream iStream = socket.getInputStream(); ;
String currentDateandTime = sdf.format(new Date());
// int filenamelen =iStream.read();
//image Name
byte[] countBuf = new byte[8];
// byte[] imageNameByte = new byte[7];
iStream.read(countBuf);
//iStream.read(imageNameByte,0,7);
readTxt = new String(countBuf);
// imageName= new String(imageNameByte);
int size = Integer.parseInt(readTxt);
//Send byte array
senderSocket.Send(countBuf, 0, data.Length, SocketFlags.None);new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "SPARCS" + File.separator + folderName + File.separator + fileName).mkdirs();
Log.e("TCP", "Create Drkt");
//Create Image(File)
new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "SPARCS" + File.separator + folderName + File.separator + fileName, imageName + ".jpg").createNewFile();
Log.e("TCP", "Create Img");
try {
FileOutputStream fOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "SPARCS" + File.separator + folderName + File.separator + fileName + File.separator + imageName + ".jpg");
BufferedOutputStream BufOutputStream = new BufferedOutputStream(fOutputStream);
byte[] aByte = new byte[size];
int byteRead;
// int bytesRead = iStream.read(aByte);
//Read from server
while ((byteRead = iStream.read(aByte)) > 0) {
Log.e("TCP", "Save to file");//Write to file
BufOutputStream.write(aByte, 0, byteRead);
}
publishProgress();
notifyID++;
// String imageUri =Environment.getExternalStorageDirectory() + File.separator + "myDirectory" + File.separator + readTxt + File.separator+currentDateandTime+".jpg";
BufOutputStream.flush();
BufOutputStream.close();
socket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
答案 2 :(得分:2)
很简单:
String str = "abciabehgitye";
str = str.replaceAll("[aei]", "*");
System.out.println("str = " + str); // str = *bc**b*hg*ty*
答案 3 :(得分:1)
for循环中sentence
的修改是错误的,它会改变(增加)字符串的长度,导致无限的循环。
按如下方式更改:
if (sentence.charAt(i)=='a' || sentence.charAt(i)=='e' || sentence.charAt(i)=='i') {
sentence = sentence.substring(0,i) + "*" + sentence.substring(i+1,sentence.length());
}
这将输出正确的结果。
您也可以使用字符串replace / replaceAll方法来获得结果。