How to find int, uppercase, lowercase, whitespace and numbers in a string
SOLVED, thanks to everyone who helped!
I'm new to java and I've been stuck with this issue for a long time ... whenever I execute my code it compiles, but when I run it and put in a line (any line) 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)
Can someone please help me? 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);
}
}
source to share
You must change
for (i = 0; i <= answer.length(); i++) ;
to
for (i = 0; i <answer.length(); i++) ;
There is no match for answer.charAt(answer.length())
, and not only
You need to delete
for(i = 0; i <= answer.length(); i++); <==remove ;
Then the for loop should like
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++;
}
source to share
you get a range error because the pointer was out of index answer
. the array starts at 0 and ends at array.length()-1
, so you need to change the code and delete =
at <=
, after which it will go tolength-1
and for the odometer, you need to compare the i-th character of yours answer
with a space, that means you need to replace if(i == ' ')
with if(answer.charAt(i) == ' ')
.
this 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);
}
}
source to share