0

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);
}
ArunKumar M N
  • 1,256
  • 1
  • 10
  • 22
Meme
  • 23
  • 1
  • 5
  • Your code gives me an exception: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.charAt(Unknown Source) at com.olsa.amex.interfaces.GeneratorMain.main(GeneratorMain.java:65) Basically at the line `senChar = senStr.charAt(i)` – vc73 Apr 23 '19 at 09:25
  • for me there is no output and I have to close Putty and open it again!! – Meme Apr 23 '19 at 09:27
  • `while (i < senLength);` is an infinite loop – Thum Choon Tat Apr 23 '19 at 09:29
  • I've copied your code, and I noticed there is a ";" right after the while that blocks the execution, fix your code's format properly. If you're using Eclipse you can just select the code and then press "CTRL + SHIFT + F" – vc73 Apr 23 '19 at 09:29
  • There is a very bad logic behind the "while" loop, you're basically iterating over the string length "Mi4", but then you do a "substring" in the while, changing completely the word into a smaller one, and that causes the Exception – vc73 Apr 23 '19 at 09:33
  • One error is that you extract a length-1 substring from the input and get the `i`-th character from that. Second error is that in case of a capital, you replace the result instead of adding to it. – Mark Jeronimus Apr 23 '19 at 09:40
  • @vc73 I know it is very bad logic because I'm student and I'm not allowed to use advanced commands. Thank you. – Meme Apr 23 '19 at 09:49
  • It's not about advanced commands, it's about the logic behind it – vc73 Apr 23 '19 at 09:52

2 Answers2

0

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());
    }
PVR
  • 885
  • 9
  • 18
0

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);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Syed Mehtab Hassan
  • 1,297
  • 1
  • 9
  • 23