循环内的字符串混乱了

huangapple go评论63阅读模式
英文:

Strings getting messed up within the loop

问题

package Uri;
import java.util.Scanner;
import java.lang.Math;
import java.lang.StringBuilder;

public class Facil{
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNext()){
            
            String lida=sc.nextLine();
            
            int letras=0, palavras=0;
       
            while(!lida.equals("")){
                
                System.out.println("current sentence = " + lida);
                
                if (lida.charAt(0) == ' ') {
                    lida = lida.replaceFirst(" ", "");
                    continue;
                }
                if (!lida.contains(" ")) {
                    palavras++;
                    letras+=lida.length();
                    System.out.println("quantity of added letters = " + lida.length());
                    break;
                }
                
                String separada = lida.substring(0,lida.indexOf(" "));
                System.out.println("separated word = " + separada);
                
                if (!separada.contains("..") && separada.endsWith(".") || !separada.contains(".")){
                    palavras++;
                    letras+=separada.length();
                    lida = lida.replace(separada, "");
                    System.out.println("added letters = " + separada.length());
        
                } 
            }
            System.out.println("\n RESULT: ");
            System.out.println("quantity of words = " + palavras);
            System.out.println("quantity of letters = " + letras);
            
        }
    }
}
英文:

So guys, I've made a program that counts how many words and letters a given sentence has. For instance, if my input is "stack overflow", it would return 2 WORDS and 13 LETTERS. But I've been having a problem to read single letters that are also contained in another word within the sentence. For example, the input "a alex" has been returning me 2 words and 4 LETTERS, when it should be returning 5 letters... I noticed that, as soon as it reads the first "a", the second string "alex" becomes "lex" for some reason, but I don't know how to solve it... here's the snippet, thx in advance!

package Uri;
import java.util.Scanner;
import java.lang.Math;
import java.lang.StringBuilder;
public class Facil{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String lida=sc.nextLine();
int letras=0, palavras=0;
while(lida != ""){
System.out.println("current sentence = " + lida);
if (lida.charAt(0) == ' ') {
lida = lida.replaceFirst(" ", "");
continue;
}
if (!lida.contains(" ")) {
palavras++;
letras+=lida.length();
System.out.println("quantity of added letters = " + lida.length());
break;
}
String separada = lida.substring(0,lida.indexOf(" "));
System.out.println("separated word = " + separada);
if (!separada.contains("..") && separada.endsWith(".") || !separada.contains(".")){
palavras++;
letras+=separada.length();
lida = lida.replace(separada, "");
System.out.println("added letters = " + separada.length());
} 
}
System.out.println("\n RESULT: ");
System.out.println("quantity of words = " + palavras);
System.out.println("quantity of letters = " + letras);
}
}
}

答案1

得分: 0

你的问题出在 lida = lida.replace(separada, ""); 这一行,你在这里将 'a' 替换为空格。

要不考虑使用 lida.split(" "),然后获取数组的长度以获得单词数?对于每个单词,使用 String.length() 来计算字母数呢?

英文:

Your problem comes from lida = lida.replace(separada, ""); where you replace 'a' with space

How about using lida.split(" ") then get the length of the array for the number of words? And for each word, use String.length() to count for the letters?

huangapple
  • 本文由 发表于 2020年8月20日 09:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63497171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定