I need to check if the array of character [i] equal space or not
Please look at the code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("\tplease enter text");
System.out.print("\t");
String text = in.next();
for(int i =0;i<text.length();i )
{
if(text.charAt(i) == 'a') // case a
{
System.out.println("it is > a");
}
else if(text.charAt(i) == ' ') // case space
{
System.out.println("it is a > space");
}
}
}// main
}
Whene (text.charAt(i) == ' ') TRUE It doesn't do the statement
CodePudding user response:
text.charAt(i) == ' '
will not be true since you use Scanner
's next
method and thus your text
variable will not contain a space.
By default a Scanner
's delimeter is whitespace, so your call of
String text = in.next();
will cause text
to only contain data up until the first space of your input. You can verify this using a System.out.println(text)
.
Use String text = in.nextLine();
instead.