I am working with Java. The problem is as follows, this can not be solved using
ID: 3804150 • Letter: I
Question
I am working with Java. The problem is as follows, this can not be solved using for/ while loops as we have not progressed to learn those as of yet. It wants us to use isWhitespace(c) and Character.isWhitespace(passCode.charAt(0) but im doing something wrong and I dont know what it is.
Write code to print the location of any space in the 2-character string passCode. Each space detected should print a separate statement followed by a newline. If no space exists, the program should not print anything. Sample output for the given program: The given is below: import java.util.Scanner; public class FindSpaces { public static void main (String [] args) { String passCode = ""; passCode = "A ";
Explanation / Answer
/**The class FindSpaces that checks for
* the space in the 2 character
* string variable and print
* the position of space in the two character
* */
//FindSpaces.java
public class FindSpaces
{
public static void main (String [] args)
{
//Set single space in the passCode at index=0
String passCode ="A ";
System.out.println("For passCode ="+passCode);
//using isWhiteSpace on Character class to
//test char at 0 is a space and
//character at index 1 is a space
if(Character.isWhitespace(passCode.charAt(0)) &&
Character.isWhitespace(passCode.charAt(1)))
System.out.println("Space at 0 Space at 1");
//Checking if character at index=0 is a space
else if(Character.isWhitespace(passCode.charAt(0)))
System.out.println("Space at 0 ");
//Checking if character at index=1 is a space
else if(Character.isWhitespace(passCode.charAt(1)))
System.out.println("Space at 1 ");
else
System.out.println("No space");
}
}//end of class
/*---------------------------------*/
Sample output:
For passCode =A
Space at 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.