Search code examples
javastringfor-loopcomparecompareto

Comparing Strings ( the user types as many as he wants) and then telling which one is first (based on which first letter comes first) using for loop


First I have to ask the user to type a number. That number will decide how many sentences he has to type (should be >2 ) and then I have to compare those sentences. I have to tell which one comes first ( based on alphabet letters order). This is all I have done so far. I don't know how to compare Strings since I don't know how many the user will type and don't have names for them.

import java.util.*;

public class Sentences {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int number = 0;
        while (number < 2) {
            System.out.println("Type a number > 2");
            number = scan.nextInt();
        }

        scan.nextLine();

        String sentence;
        int y = 0;
        for (int i = 0; i < number; i++) {
            System.out.println("Type a sentence");
            sentence = scan.nextLine();
        }
    }
}

Solution

  • You were close. Instead of a single variable(sentence), you need an array(sentences[]) as shown below:

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int number = 0;
            while (number < 2) {
                System.out.print("Type a number > 2: ");
                number = Integer.parseInt(scan.nextLine());
            }
    
            String[] sentences = new String[number];
            int y = 0;
            for (int i = 0; i < number; i++) {
                System.out.print("Type a sentence: ");
                sentences[i] = scan.nextLine();
            }
            Arrays.sort(sentences);
            for (String sentence : sentences) {
                System.out.println(sentence);
            }
        }
    }
    

    To sort String sentences[], you need to use Arrays.sort(sentences) as shown above.

    A sample run:

    Type a number > 2: 0
    Type a number > 2: 3
    Type a sentence: Hello world
    Type a sentence: You are awesome
    Type a sentence: My name is Jane
    Hello world
    My name is Jane
    You are awesome
    

    [Update]

    As per your clarification, you wanted to print only one sentence which is the lowest in alphabetical order. It is like tracking the minimum number from a list of numbers.

    The algorithm is as follows:

    1. Store the first input to a variable, say lowestAlphabetical.
    2. In a loop, compare the value of lowestAlphabetical with the next input and if the next input is lower in alphabetical order put the next input into lowestAlphabetical.

    Demo:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int number = 0;
            while (number < 2) {
                System.out.print("Type a number > 2: ");
                number = Integer.parseInt(scan.nextLine());
            }
    
            // Scan the first sentence
            System.out.print("Type a sentence: ");
            String sentence = scan.nextLine();
    
            // Since we have only one sentence till now, it is also the lowest in
            // alphabetical order
            String lowestAlphabetical = sentence;
    
            // Loop for next inputs
            for (int i = 1; i < number; i++) {
                System.out.print("Type the next sentence: ");
                sentence = scan.nextLine();
                if (sentence.compareTo(lowestAlphabetical) < 0) {
                    lowestAlphabetical = sentence;
                }
            }
            System.out.println(lowestAlphabetical);
        }
    }
    

    A sample run:

    Type a number > 2: 3
    Type a sentence: Hello world
    Type the next sentence: Good morning
    Type the next sentence: My name is Jane
    Good morning