Java 拆线
嗨,伙计们,我正忙着断/分线。 但是字符串不是固定的,因此当输入发生变化时,程序仍然必须处理任何字符输入 到现在为止,我走了这么远,但我迷路了 我制作了一个字符数组,并将数组的大小设置为将作为输入获取的任何字符串的长度。我做了一个for循环来循环字符串中的字符 因为我知道我的字符串还不在数组中,所以现在如何将字符串插入数组中?然后,当它最终遍历字符串的字符时,必须在不同的行上打印出数字和操作数。所以输出在本例中看起来是这样的 一, + 三, , 432 123 等 我想在不使用匹配器、扫描仪等的情况下实现这一点。我想使用基本的Java技术,就像您在HeadfirstJava的前3章中学习的那样Java 拆线,java,arrays,string,for-loop,Java,Arrays,String,For Loop,嗨,伙计们,我正忙着断/分线。 但是字符串不是固定的,因此当输入发生变化时,程序仍然必须处理任何字符输入 到现在为止,我走了这么远,但我迷路了 我制作了一个字符数组,并将数组的大小设置为将作为输入获取的任何字符串的长度。我做了一个for循环来循环字符串中的字符 因为我知道我的字符串还不在数组中,所以现在如何将字符串插入数组中?然后,当它最终遍历字符串的字符时,必须在不同的行上打印出数字和操作数。所以输出在本例中看起来是这样的 一, + 三, , 432 123 等 我想在不使用匹配器、扫描仪等
public class CharAtExample {
public static void main(String[] args) {
// This is the string we are going to break down
String inputString = "1+3,432.123*4535-24.4";
int stringLength = inputString.length();
char[] destArray = new char[stringLength];{
for (int i=0; i<stringLength; i++);
}
公共类字符示例{
公共静态void main(字符串[]args){
//这是我们要拆开的绳子
String inputString=“1+3432.123*4535-24.4”;
int stringLength=inputString.length();
char[]destArray=新字符[stringLength]{
对于(int i=0;i这里是一个可能的解决方案,我们逐个字符地添加到一个现有字符串中,该字符串将成为我们的数字,或者它将字符串添加到数组中,清除当前数字,然后添加特殊字符。最后,我们在数组中循环找到数字或非数字字符的次数。我使用了ASCII表将字符标识为数字,在您的编程生涯中,该表将非常有用。最后,我将数组更改为字符串数组,因为字符不能容纳像“432”这样的数字,只能容纳“4”或“3”或“2”
String inputString = "1+3,432.123*4535-24.4";
int stringLength = inputString.length();
String[] destArray = new String[stringLength];
int destArrayCount = 0;
String currentString = "";
for (int i=0; i<stringLength; i++)
{
//check it's ascii value if its between 0 (48) and 9 (57)
if(inputString.charAt(i) >= 48 && inputString.charAt(i) <= 57 )
{
currentString += inputString.charAt(i);
}
else
{
destArray[destArrayCount++] = currentString;
currentString = "";
//we know we don't have a number at i so its a non-number character, add it
destArray[destArrayCount++] = "" + inputString.charAt(i);
}
}
//add the last remaining number
destArray[destArrayCount++] = currentString;
for(int i = 0; i < destArrayCount; i++)
{
System.out.println("(" + i + "): " + destArray[i]);
}
String inputString=“1+3432.123*4535-24.4”;
int stringLength=inputString.length();
String[]destArray=新字符串[stringLength];
int destArrayCount=0;
字符串currentString=“”;
对于(int i=0;i这里是一个可能的解决方案,我们逐个字符地添加到一个现有字符串中,该字符串将成为我们的数字,或者它将字符串添加到数组中,清除当前数字,然后添加特殊字符。最后,我们在数组中循环找到数字或非数字字符的次数。我使用了ASCII表将字符标识为数字,在您的编程生涯中,该表将非常有用。最后,我将数组更改为字符串数组,因为字符不能容纳像“432”这样的数字,只能容纳“4”或“3”或“2”
String inputString = "1+3,432.123*4535-24.4";
int stringLength = inputString.length();
String[] destArray = new String[stringLength];
int destArrayCount = 0;
String currentString = "";
for (int i=0; i<stringLength; i++)
{
//check it's ascii value if its between 0 (48) and 9 (57)
if(inputString.charAt(i) >= 48 && inputString.charAt(i) <= 57 )
{
currentString += inputString.charAt(i);
}
else
{
destArray[destArrayCount++] = currentString;
currentString = "";
//we know we don't have a number at i so its a non-number character, add it
destArray[destArrayCount++] = "" + inputString.charAt(i);
}
}
//add the last remaining number
destArray[destArrayCount++] = currentString;
for(int i = 0; i < destArrayCount; i++)
{
System.out.println("(" + i + "): " + destArray[i]);
}
String inputString=“1+3432.123*4535-24.4”;
int stringLength=inputString.length();
String[]destArray=新字符串[stringLength];
int destArrayCount=0;
字符串currentString=“”;
对于(int i=0;i您可以使用Character.isDigit(char)
来区分数字字符和非数字字符,因为实际上这是在同一行中对多个字符进行分组的唯一标准。
它将提供:
public static void main(String[] args) {
String inputString = "1+3,432.123*4535-24.4";
String currentSequence = "";
for (int i = 0; i < inputString.length(); i++) {
char currentChar = inputString.charAt(i);
if (Character.isDigit(currentChar)) {
currentSequence += currentChar;
continue;
}
System.out.println(currentSequence);
System.out.println(currentChar);
currentSequence = "";
}
// print the current sequence that is a number if not printed yet
if (!currentSequence.equals("")) {
System.out.println(currentSequence);
}
}
或者,您可以通过检查char
的int
值是否包含在数字的十进制值范围内,将其编码到更低的级别:
if(currentChar >= 48 && currentChar <= 57 ) {
如果(currentChar>=48&¤tChar您可以使用Character.isDigit(char)
来区分数字字符和非数字字符,因为实际上这是在同一行中分组多个字符的单一标准。
它将提供:
public static void main(String[] args) {
String inputString = "1+3,432.123*4535-24.4";
String currentSequence = "";
for (int i = 0; i < inputString.length(); i++) {
char currentChar = inputString.charAt(i);
if (Character.isDigit(currentChar)) {
currentSequence += currentChar;
continue;
}
System.out.println(currentSequence);
System.out.println(currentChar);
currentSequence = "";
}
// print the current sequence that is a number if not printed yet
if (!currentSequence.equals("")) {
System.out.println(currentSequence);
}
}
或者,您可以通过检查char
的int
值是否包含在数字的十进制值范围内,将其编码到更低的级别:
if(currentChar >= 48 && currentChar <= 57 ) {
如果(currentChar>=48&¤tChar这比你想象的要容易。
首先:要获得包含字符串字符的数组,只需使用所有字符串都具有的tocharray()
方法。
例如myString.tocharray()
第二:当您看到一个字符不是数字时,您希望移动到下一行,打印该字符,然后再次移动到下一行。
以下代码正是这样做的:
public class JavaApplication255 {
public static void main(String[] args) {
String inputString = "1+3,432.123*4535-24.4";
char[] destArray = inputString.toCharArray();
for (int i = 0 ; i < destArray.length ; i++){
char c = destArray[i];
if (isBreakCharacter(c)){
System.out.println("\n" + c);
} else {
System.out.print(c);
}
}
}
public static boolean isBreakCharacter(char c){
return c == '+' || c == '*' || c == '-' || c == '.' || c == ',' ;
}
公共类JavaApplication255{
公共静态void main(字符串[]args){
String inputString=“1+3432.123*4535-24.4”;
char[]destArray=inputString.ToCharray();
for(int i=0;i这比你想象的要容易。
首先:要获得包含字符串字符的数组,只需使用所有字符串都具有的tocharray()
方法。
例如myString.tocharray()
第二:当您看到一个字符不是数字时,您希望移动到下一行,打印该字符,然后再次移动到下一行。
以下代码正是这样做的:
public class JavaApplication255 {
public static void main(String[] args) {
String inputString = "1+3,432.123*4535-24.4";
char[] destArray = inputString.toCharArray();
for (int i = 0 ; i < destArray.length ; i++){
char c = destArray[i];
if (isBreakCharacter(c)){
System.out.println("\n" + c);
} else {
System.out.print(c);
}
}
}
public static boolean isBreakCharacter(char c){
return c == '+' || c == '*' || c == '-' || c == '.' || c == ',' ;
}
公共类JavaApplication255{
公共静态void main(字符串[]args){
String inputString=“1+3432.123*4535-24.4”;
char[]destArray=inputString.ToCharray();
for(int i=0;i
我现在如何将字符串插入数组
不确定这是什么意思。您希望最终结果是什么样的?我的输出在我的帖子中“在这种情况下输出是这样的”你是在问我们如何将字符串解析成令牌吗?如果是,请做一些研究,因为有很多文章都在讨论如何进行解析。好的