-2

INSTRUCTIONS:

Write an application that accepts any number of String values from a user until they enter zzz or have entered 15 strings, and display them in ascending order (alphabetical order).

Case should be a factor when ordering the values. For example, uppercase 'B' comes before lowercase 'a'.

PROBLEM:

The loop won't stop when I input "zzz"

After filling 15 strings: The console will prompt "Array Index OutOfBoundsException:15

import java.util.*;
public class StringSort2 {
    public static void main(String[] args) {

        String[] hatdog = new String[15]; 
        Scanner input = new Scanner(System.in);
        
        for (int i = 0; i <= hatdog.length; i++){
            String words = input.nextLine();
            if(words != "zzz")
                 hatdog[i] = words;
            else if (words == "zzz")
                break;
                }

        Arrays.sort(hatdog);
        for(int j=0; j<hatdog.length; j++)
             System.out.println(hatdog[j]);
             
    }
}
R.E.F.
  • 37
  • 7

1 Answers1

0

hatdog.length = 15, so the largest index is 14. You are looping up to index 15, which causes the error.

You should compare strings using String.equals() instead of ==

You could simplify the condition as:

if(words.equals("zzz") {
    break;
}
hatdog[i] = words;

EDIT: If you want to be a boss, you could do it using streams in a single line

    new BufferedReader(new InputStreamReader(System.in)) {

        @Override
        public String readLine() throws IOException {
            String line = super.readLine();
            if (line.equals("zzz")) {
                line = null;
            }
            return line;
        }
    }.lines().limit(5).sorted().forEach(System.out::println);

EDIT 2: Explanation

   // System.in is an InputStream
   // InputStreamReader can read it
   // BufferedReader buffers the reading
   // By adding a class body with '{' I make this an anonymous
   // inner class (AIC), and can override methods
   new BufferedReader(new InputStreamReader(System.in)) {

        // the AIC overrides readLine to intercept when "zzz" 
        // is entered
        @Override
        public String readLine() throws IOException {
            // super.readLine() is the original implementation 
            // of this method in the super class
            String line = super.readLine();

            // once null is returned, the inputstream terminates
            if (line.equals("zzz")) {                   
                line = null;
            }
            return line;
        }
    }
    // a convenience method which returns the contents 
    // of a BufferedReader as a stream
    .lines()
    // limits the items in the stream to 15
    .limit(15)
    // sorts the stream by its natural order
    .sorted()
    // iterates over each stream item to print it
    .forEach(System.out::println);

EDIT 3: A slightly more mainstream solution

    List<String> lines = new ArrayList<>();
    Scanner input = new Scanner(System.in);
    String line = null;
    while (lines.size() < 15 && !"zzz".equals(line)) {
        line = input.nextLine();
        lines.add(line);
    }
    lines.sort(Comparator.naturalOrder());
    lines.forEach(System.out::println);
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60