猿问

分解任何字符串

大家好,我正忙于打破/拆分字符串。然而,字符串不是固定的,所以当输入改变时,程序仍然必须处理任何字符输入。


到目前为止,我已经走了这么远,但我迷路了。


我制作了一个字符数组,并将数组的大小设置为等于将作为输入获取的任何字符串的长度。我做了一个 for 循环来遍历字符串的字符。


我现在如何将我的字符串插入到数组中,因为我知道我的字符串还没有在那里?然后,当它最终遍历我的字符串的字符时,必须在不同的行上打印出数字和操作数。所以在这种情况下输出看起来像这样;


1


+


3


,


432


.


123


等等


我想在不使用匹配器、扫描器等的情况下做到这一点。我想使用基本的 Java 技术,就像你在 HeadfirstJava 的前 3 章中学到的那样。


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++);

    }


慕无忌1623718
浏览 178回答 3
3回答

神不在的星期二

您可以使用Character.isDigit(char)区分数字和非数字字符,因为实际上这是在同一行中对多个字符进行分组的单一标准。它会给:public static void main(String[] args) {&nbsp; &nbsp; String inputString = "1+3,432.123*4535-24.4";&nbsp; &nbsp; String currentSequence = "";&nbsp; &nbsp; for (int i = 0; i < inputString.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; char currentChar = inputString.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; if (Character.isDigit(currentChar)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSequence += currentChar;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(currentSequence);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(currentChar);&nbsp; &nbsp; &nbsp; &nbsp; currentSequence = "";&nbsp; &nbsp; }&nbsp; &nbsp; // print the current sequence that is a number if not printed yet&nbsp; &nbsp; if (!currentSequence.equals("")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(currentSequence);&nbsp; &nbsp; }}Character.isDigit()依赖于 unicode 类别。您可以自己编写代码,例如:if (Character.getType(currentChar) == Character.DECIMAL_DIGIT_NUMBER) {...}或者,您可以通过检查 的int值char是否包含在数字的ASCII十进制值范围内,在较低级别对其进行编码:if(currentChar >= 48 && currentChar <= 57 ) {它输出你想要的:1+3,432.123*4535——24.4

潇潇雨雨

这比你想象的要容易。首先:要获取包含字符串字符的数组,您只需使用toCharArray()所有字符串都具有的方法。前任。myString.toCharArray()第二:当你看到一个字符不是数字时,你想移动到下一行,打印字符,然后再次移动到下一行。以下代码正是这样做的:public class JavaApplication255 {public static void main(String[] args) {&nbsp; &nbsp; String inputString = "1+3,432.123*4535-24.4";&nbsp; &nbsp;&nbsp; &nbsp; char[] destArray = inputString.toCharArray();&nbsp; &nbsp; for (int i = 0 ; i < destArray.length ; i++){&nbsp; &nbsp; &nbsp; &nbsp; char c = destArray[i];&nbsp; &nbsp; &nbsp; &nbsp; if (isBreakCharacter(c)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\n" + c);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(c);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public static boolean isBreakCharacter(char c){&nbsp; &nbsp; return c == '+' || c == '*' || c == '-' || c == '.' || c == ',' ;}&nbsp;

aluckdog

这是一个可能的解决方案,我们逐个字符地添加到将成为我们的数字的现有字符串中,或者将字符串添加到数组中,清除当前数字,然后添加特殊字符。最后,我们遍历数组的次数与找到数字或非数字字符的次数相同。我使用 ASCII 表将字符识别为数字,该表将在您的整个编程生涯中派上用场。最后,我将数组更改为字符串数组,因为字符不能包含“432”之类的数字,只能包含“4”或“3”或“2”。String inputString = "1+3,432.123*4535-24.4";int stringLength = inputString.length();&nbsp; &nbsp;&nbsp;String[] destArray = new String[stringLength];int destArrayCount = 0;String currentString = "";for (int i=0; i<stringLength; i++){&nbsp; &nbsp; //check it's ascii value if its between 0 (48) and 9 (57)&nbsp; &nbsp; if(inputString.charAt(i) >= 48 && inputString.charAt(i) <= 57 )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; currentString += inputString.charAt(i);&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; destArray[destArrayCount++] = currentString;&nbsp; &nbsp; &nbsp; &nbsp; currentString = "";&nbsp; &nbsp; &nbsp; &nbsp; //we know we don't have a number at i so its a non-number character, add it&nbsp; &nbsp; &nbsp; &nbsp; destArray[destArrayCount++] = "" + inputString.charAt(i);&nbsp; &nbsp; }}//add the last remaining numberdestArray[destArrayCount++] = currentString;for(int i = 0; i < destArrayCount; i++){&nbsp; &nbsp; System.out.println("(" + i + "): " + destArray[i]);}重要- 如果使用某种类型的字符串,此算法将失败。你能找到这个算法失败的字符串吗?你能做些什么来确保计数总是正确的,而不是有时比实际计数大 1?
随时随地看视频慕课网APP

相关分类

Java
我要回答