Reputation: 71
The for loops in the code below are first looping through the user entries, then checking the number of spaces " " in the string. After checking through them a question is asked if they want to display strings with no spaces, one, or more. I don't think the loop is making it past the first instance of the for loop as the second should be looking for a 0 value on the count variable. Below is the nested for loops:
if(answer.equals("No")){
for (int i = 0; i < array.length;i++) {
if (array[i] != null) {
for (int count = 0; array[i].charAt(i) != ' ';count++) {
if(count == 0)
System.out.println(array[i]+" ");
}
}
}
}
Primary code for reference:
import java.util.*;
import java.util.Scanner;
public class StringsWithSpaces {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String[] array = new String[20];
System.out.println("Please enter anything..., or type QUIT to quit.");
for (int i = 0; i < array.length; i++) {
array[i] = s.nextLine();
boolean result = Arrays.stream(array).anyMatch("QUIT"::equals);
if(result)
{
break;
}
}
String str = null;
int len = -1;
System.out.println("Would you like to display strings with No Spaces, One Space or More? Type No, One, More to see the results: ");
String answer = s.nextLine();
if(answer.equals("No")){
for (int i = 0; i < array.length;i++) {
if (array[i] != null) {
for (int count = 0; array[i].charAt(i) != ' ';count++) {
if(count == 0)
System.out.println(array[i]+" ");
}
}
}
}
else if(answer.equals("One"))
{
for (int i = 0; i < array.length;i++) {
int count = 0;
if (array[i] != null) {
if (array[i].charAt(i) != ' ') {
count++;
System.out.println(count);
}
//System.out.print(array[i] + " ");
}
}
}
else
System.out.println("No values to show");
System.out.println();
}
}
I have looked for something similar, but could only find other languages. Thanks for all the help.
Upvotes: 0
Views: 42
Reputation: 317
In the first set of nested loops you're basically checking the same character of each word each time. You should use count as the parameter for the charAt method instead of whatever index is in i; Also you should only print out the word when count equals the length of the word minus one.
Here it is:
if(answer.equals("No")){
for (int i = 0; i < array.length;i++) {
if (array[i] != null) {
for (int count = 0; array[i].charAt(i) != ' ' ;count++) {
if(count == array[i].length()-1) {
System.out.println(array[i]);
break;
}
}
}
}
}
I also rewrote the second set of nested loops, it should explain itself:
else if(answer.equals("One"))
{
for (int i = 0; i < array.length;i++) {
int count = 0;
for (int j = 0; j < array[i].length(); j++) {
if (Character.isSpaceChar(array[i].charAt(j)))
count++;
}
if (count == 1) {
System.out.println(array[i]);
}
}
}
EDIT: I forgot to mention that I added the break statement that you forgot. It ensures that the for loop is exited when you reach the last character in the string. Without this statement your application would crash each time it hits the end of a string because of an IndexOutOfBoundsException.
Upvotes: 2