MG Ianoma
MG Ianoma

Reputation: 13

Java While loop condition isn't being exicuted

I'm working on a project for school but I can't figure out why my code isn't exiting when I type "zzz". it's probably simple and I'll likely feel dumb when I know what the problem is. here's my code:

import java.util.*;
public class StringSort2 {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int counter = 0;
    String[] arr = new String[15];
    System.out.println("Enter item or type 'zzz' to quit");
    arr[0] = input.nextLine();
    counter++;

     do{     
         arr[counter] = input.nextLine();
         if (input.equals("zzz")){
             System.out.println("bleh");
         }
         counter++;

     } while (!input.equals("zzz") && counter <= 14);

     Arrays.sort(arr);
     System.out.println("Array is " + Arrays.toString(arr));
} 

Upvotes: 1

Views: 196

Answers (4)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79015

Replace

if (arr[counter].equals("zzz")) {
    System.out.println("bleh");
}
counter++;

with

if (arr[counter].equals("zzz")) {
    System.out.println("bleh");
} else {
    counter++;
}

and

while (!input.equals("zzz") && counter <= 14)

with

while (!arr[counter].equals("zzz") && counter <= 14)

Explanation: zzz is a string which you have to compare with the input string stored in arr[counter].

Also, in order to avoid NullPointerException, you should perform sort operation on the copy of the array without any null element. Given below is the complete program:

import java.util.Arrays;
import java.util.Scanner;

public class StringSort2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int counter = 0;
        String[] arr = new String[15];
        do {
            System.out.print("Enter item or type 'zzz' to quit: ");
            arr[counter] = input.nextLine();
            if (arr[counter].equals("zzz")) {
                System.out.println("bleh");
            } else {
                counter++;
            }
        } while (!"zzz".equals(arr[counter]) && counter <= 14);
        arr = Arrays.copyOf(arr, counter);
        Arrays.sort(arr);
        System.out.println("Array is " + Arrays.toString(arr));
    }
}

A sample run:

Enter item or type 'zzz' to quit: a
Enter item or type 'zzz' to quit: b
Enter item or type 'zzz' to quit: c
Enter item or type 'zzz' to quit: zzz
bleh
Array is [a, b, c]

Upvotes: 2

OneCricketeer
OneCricketeer

Reputation: 191701

input is a Scanner. It will never equal a String.

Try this

Scanner sc = new Scanner(System.in);
String input;  // this is now a proper string to compare

String[] arr = new String[15];

System.out.println("Enter item or type 'zzz' to quit");
input = sc.nextLine();

int counter = 0;
while (counter < arr.length) {     
   if (input.equals("zzz")) return;  // check most recently entered input
   arr[counter++] = input; // if not returned, store in the list and increase counter
   input = sc.nextLine();  // prompt next line
}
Arrays.sort(arr);
System.out.println("Array is " + Arrays.toString(arr));

Upvotes: 1

Jaeheon Shim
Jaeheon Shim

Reputation: 501

The reason your code isn't working is that input is the scanner object, and the equals method on the scanner object doesn't refer to the data being read by it. A way of doing this so that it works would be:

import java.util.*;
public class Help {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int counter = 0;
        String[] arr = new String[15];
        String inputString = null;

        System.out.println("Enter item or type 'zzz' to quit");
        do{     
            inputString = input.nextLine();
            arr[counter] = inputString;
            if (inputString.equals("zzz")){System.out.println("bleh");}
            counter++;

        }while (!inputString.equals("zzz") && counter <= 14);

        Arrays.sort(arr);
        System.out.println("Array is " + Arrays.toString(arr));
    } 
}

Upvotes: 1

omoshiroiii
omoshiroiii

Reputation: 693

import java.util.*;
public class StringSort2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int counter = 0;
        String[] arr = new String[15];
        System.out.println("Enter item or type 'zzz' to quit");
        arr[counter] = input.nextLine();
        counter++;

         do{     
              arr[counter] = input.nextLine();
             if (arr[counter].equals("zzz")){System.out.println("bleh");}
                counter++;

        }while (!arr[counter].equals("zzz") && counter <= 14);

         Arrays.sort(arr);
        System.out.println("Array is " + Arrays.toString(arr));
    } 

}

You meant to compare the input String, not the Scanner object. I also removed your magic number "0" index that you had , since you already set your counter to 0.

Upvotes: 2

Related Questions