简体   繁体   中英

how to solve no output problem -using while-?

I try to read each char of "Mi4" if it is letter put it in variable "capital" if it is number put it in variable "num" there is no error, and NO output "

public static void main(String[] args) {
    String capital = "";
    int num = 1;
    String sentence = "Mi4";
    int senLength = sentence.length();
    int i = 0;
    while (i < senLength) {
        String senStr = sentence.substring(i, i + 1);
        char senChar = senStr.charAt(i);
        if (senChar >= 'A' && senChar <= 'Z') {
            capital = senStr;
        } else if (senChar >= 'a' && senChar <= 'z') {
            capital = capital + senStr;
        } else if (senChar >= '2' && senChar <= '9') {
            num = Integer.parseInt(senStr);
        }
        i++;
        sentence = sentence.substring(i);
    }
    System.out.println(capital);
    System.out.println(num);
}

You can try this code, What i can understand from your code is that you have to iterate over character of String and separate out characters and numbers.

So iterating While loop till the length and fetching character using charAt(index) will suffice you requirement.

Instead of appending String, always use StringBuffer/StringBuilder. You can go through this link

If you want to parse character in Integer then convert it to string and then Integer. You can see commented code.

public static void main(String[] args) {
        //String capital = "";
        //int num = 1;
        String senStr = "Mi4";
        int senLength = senStr.length();
        int i = 0;

        StringBuffer chBr =new StringBuffer();
        StringBuffer numBr =new StringBuffer();
        while (i < senLength)
        {
            char senChar = senStr.charAt(i);
            if (senChar >= 'A' && senChar <= 'Z')
            {
                chBr.append(senChar);
            }
            else if (senChar >= 'a' && senChar <= 'z')
            {
                chBr.append(senChar);
            }
            else if (senChar >= '2' && senChar <= '9')
            {
                //num =Integer.parseInt(String.valueOf(senChar));
                numBr.append(senChar);
            }
            i++;
            //sentence = sentence.substring(i);
        }
        System.out.println(chBr.toString());
        System.out.println(numBr.toString());
    }

I have tried your code there were many mistake mentioned below with solution

String capital="";
int num=1;
String sentence= "Mi4";
int senLength= sentence.length();
int i=0;
while (i < senLength)//; in the while loop
{
    //String senStr=sentence.substring(i,i+1);  substring should be of 1st letter so (i,i+1) doesnot find letter in next iteration 
    char senChar= senStr.charAt(i);
    if (senChar >= 'A' && senChar <='Z')
    {capital= senStr;}
    else if (senChar >= 'a' && senChar <='z')
    {capital= capital+senStr;}
    else if (senChar >='2' && senChar<='9')
    {num= Integer.parseInt(senStr);}
    i++;
    //sentence=sentence.substring(i);same here in next iteration the string doesnot contains same lenght so it doesnot find char at i
}
System.out.println(capital);
System.out.println(num);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM