- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我正在努力为即将到来的 final 练习,我只是为了好玩和学习而随机编码。今晚我偶然发现了这个我以前从未见过的错误代码。我的代码似乎没有进入 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);
}
最佳答案
TLDR:这里是一个无限循环,它会扩展字符串“句子”,直到内存不足。但我不会确切地告诉您如何解决您的问题。
此外,您还需要学习如何在程序中使用断点和打印语句进行调试。一个简单的谷歌搜索就足够了。
为什么它不起作用的解释:
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 中的行号时,这会导致出现“断点”。这意味着如果你在“ Debug模式”下运行程序,解释器会在那一行暂停,让你看到一些信息,比如某些变量的值。调试时的另一个技巧是使用打印语句来显示有关代码中发生的事情的信息,通常以这种方式显示变量。
PS 大家都曾经这样问过问题,所以不要因为找不到问题而气馁。以后使用上面的建议先尝试调试,再尝试其他来源的解决方案。通常,像这样的简单错误是问题的原因,可以通过调试找到它们。
关于java - 我正在尝试制作一个简单的程序,用 'a' 符号替换 'e' 、 'i' 、 "*"但我收到此错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34279894/
给定一个字符串,例如 s="##$$$#",我如何找到索引之前的“#”符号数等于“”数的索引$"符号在索引之后? 示例:如果 s="##$$$#",则输出将为 2。 解释:在索引 2 之前我们有 2
在本教程中,您将借助示例了解 JavaScript 符号。 JavaScript 符号 JavaScript ES6 引入了一种新的原始数据类型,称为 Symbol(符号)。符号是不可变的(不能更改)
在“函数编程的工艺”一书中,符号 '>.>' 将函数连接在一起,与 '.' 的方向相反。但是当我使用 ghci 实现它时,它显示了超出范围的错误 '>.>'。为什么?它是不再使用的旧符号吗? 最佳答案
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我需要从向量中删除 \"。这是我的数据: data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1
我在 Nginx 配置中使用正则表达式来捕获文件 URL,但如果文件 URL 包含 # 符号,正则表达式模式将不会捕获它。 这里是nginx的配置部分。 location ~ ^/p/(?[\w\-=
如何使 & 符号在此图表的第一组条形/列下正确显示: http://jsfiddle.net/VxbrK/2/ 应该是“Apples & Oranges”而不是“Apples & Oranges”。
**在verilog中是什么意思? 我为测试台提供了以下逻辑 localparam NUM_INPUT_BITS = 1; localparam NUM_OUTPUT_BITS
我有一个使用正则表达式来验证电子邮件地址的方法。 public String searchFormail(String searchWord) { Pattern pattern = Patt
我想将一个字符串拆分为数字部分和文本/符号部分我当前的代码不包含负数或小数,并且表现得很奇怪,在输出的末尾添加了一个空列表元素 import re mystring = 'AD%5(6ag 0.33-
我有一些代码需要从数组中选择一个随机字符串,但它一直返回单个字母或数字。如何解决这个问题? var name = ["Yayek", "Vozarut", "Gezex",
我刚开始使用 Python,我在考虑应该使用哪种表示法。我读过 PEP 8关于 Python 符号的指南,我同意那里的大多数内容,除了函数名称(我更喜欢混合大小写风格)。 在 C++ 中,我使用匈牙利
在用 C# 编写代码时,我错误地在 if 语句中的变量前添加了一个符号(而不是感叹号)。 bool b = false; if (@b) { } 我很惊讶它编译成功,没有任何错误。 我想知道:上面的代
本文实例为大家分享了特殊字符替换电话号码中某一部分的方法,ios利用-号替换电话号码中间四位,供大家参考,具体内容如下 1、效果图 2、代码 rootviewcontroll
当我使用“x”和“z”作为符号时,这段代码没有问题: from sympy import * x, z = symbols('x z') y = -6*x**2 + 2*x*z**0.5 + 50*x
我需要从文本中删除标点符号: data <- "Type the command AT&W enter. in order to save the new protocol on modem;"
我有几个数字是 numeric 类。下面的例子。 df = c(12974,12412,124124,124124,34543,4576547,32235) 现在我想在每个数字前添加 '$' 符号而不
我有一个 highcharts 图例,其中符号以不同的大小显示,因为它们在实际图表中的大小不同。不幸的是,当数据点的大小增加时,它们也会在图例中增加。无论数据点大小如何,我都希望图例符号保持相同的大小
我需要使用包含平均值+-SD的标题。到目前为止,我只能得到以下信息: "Mean +- SD or N (%)" [1] "Mean +- SD or N (%)" 如何直接使用“+-”符号?您知道一
使用 XSLT 和 XPath 1.0,我有一个要转义的字符串以用于 URL,例如: one word & another 因此,描述元素的 text() 应该进行 URL 转义。 我该怎么做
我是一名优秀的程序员,十分优秀!