简体   繁体   中英

How to find int, uppercase, lowercase, space, and digits in a string

SOLVED, thanks to all that helped!!

I am new to the java language and i have been stuck on this problem for quite a while now... whenever i execute my code, it would compile but when i run it and enter a string (any string), i get this error:

java.lang.StirngIndexOutOfBoundsException: Stirng index out of range: at java.lang.String.charAt(unknown Source) at Reading.main(Reading.java:48)

could someone please help me out?? this is my code:

import java.awt.*;  
import javax.swing.*;
import java.util.*;

public class Reading{
    static Scanner input = new Scanner(System.in);
    public static void main(String [] args){
        System.out.println("Enter a string of characters: ");   
        int i, Upper=0, Lower=0, Space=0, Digits=0;
        String answer = input.nextLine();
        for(i = 0; i <= answer.length(); i++);
        {
            if(Character.isUpperCase(answer.charAt(i)))
                Upper++;
            if(Character.isLowerCase(answer.charAt(i)))
                Lower++;
            if(Character.isDigit(answer.charAt(i))) 
                Digits++;
        if (answer.charAt(i)==' ')
            Space++;
            System.out.println("There are " + Upper + " upper case letters");
            System.out.println("There are " + Lower + " lower case letters");
            System.out.println("There are " + Digits + " digits");
            System.out.println("There are " + Space + " spaces");
        }
        System.exit(0);
    }
} 

You have to change

for (i = 0; i <= answer.length(); i++) ;

to

for (i = 0; i <answer.length(); i++) ;

There is no matching for answer.charAt(answer.length()) , Not only that

You need to remove

for(i = 0; i <= answer.length(); i++); <==remove ;

Then your for loop should like this

for (i = 0; i < answer.length(); i++) {
     if (Character.isUpperCase(answer.charAt(i)))
         Upper++;
     if (Character.isLowerCase(answer.charAt(i)))
         Lower++;
     if (Character.isDigit(answer.charAt(i)))
         Digits++;
     if (i == ' ') // i is an index this should be if(answer.charAt(i)==' ')
         Space++;
 }

This is an off-by-one error.

Change

for(i = 0; i <= answer.length(); i++);

to

for(i = 0; i < answer.length(); i++);

you get out of range error because you index was out of answer s index. an array start at 0 and end at array.length()-1 so you need to change the code and remove = in <= then it will iterate to length-1

and for space counter you need to compare i'th character of your answer with space it means you need to replace if(i == ' ') with if(answer.charAt(i) == ' ') .

here is what you wanted :

import java.awt.*;
import javax.swing.*;
import java.util.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    static Scanner input = new Scanner(System.in);
    public static void main(String [] args)
    {
        System.out.println("Enter a string of characters: ");   
        int i, Upper=0, Lower=0, Space=0, Digits=0;
        String answer = input.nextLine();
        System.out.println("Enter a string of characters2: ");
        for(i = 0; i < answer.length(); i++);
        {
            if(Character.isUpperCase(answer.charAt(i)))
                Upper++;

            if(Character.isLowerCase(answer.charAt(i)))
                Lower++;

            if(Character.isDigit(answer.charAt(i))) 
                Digits++;

            if (answer.charAt(i)==' ')
            Space++;

             System.out.println("There are " + Upper + " upper case letters");
             System.out.println("There are " + Lower + " lower case letters");
             System.out.println("There are " + Digits + " digits");
             System.out.println("There are " + Space + " spaces");
        }
        System.exit(0);
    }

}

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