How to print sentences from minor to major?

Asked

Viewed 198 times

2

I need to make a class that receives 3 user sentences and prints in order from the smallest sentence to the largest:

Make a program to read three user phrases and show on screen the three sentences in order of sentence size. For example: Suppose the user provided the following phrases:

  1. Today I was cheerful
  2. Today is Friday
  3. Yesterday I was very sad

The result of the program would be:

Hoje é sexta
Hoje eu fiquei alegre
Ontem eu estava bem triste

I’ve tried two ways:

1: Using array and Collections.Sort I searched on the net and could not even understand

package exercicio1;

import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JOptionPane;

public class App {

    public static void main(String[] args) {


        ArrayList<String> lista = new ArrayList<String>();
        lista.add(JOptionPane.showInputDialog("Frase 1:"));
        lista.add(JOptionPane.showInputDialog("Frase 2:"));
        lista.add(JOptionPane.showInputDialog("Frase 3:"));

        Collections.sort(lista);

        for(int i = 0; i < lista.size(); i++){
            System.out.println(lista.get(i));
        }

    }
}

2: Using a Strings and ifs array. Obs. I managed to print the smallest of all but I have no idea how to proceed, I could facilitate this process using Math.min(frase1.length(), Math.min(frase2.length(), frase3.length length());

package exercicio1;

import javax.swing.JOptionPane;

public class App {

    public static void main(String[] args) {


        String[] frases = new String[3];

        String frase1 =  JOptionPane.showInputDialog("Frase 1:");
        String frase2 =  JOptionPane.showInputDialog("Frase 2:");
        String frase3 =  JOptionPane.showInputDialog("Frase 3:");

        if(frase1.length() < frase2.length() && frase1.length() < frase3.length()){
            frases[1] = frase1;
        }else{
            if(frase2.length() < frase1.length() && frase2.length() < frase3.length()){
                frases[1] = frase2;
            }else{
                if(frase3.length() < frase1.length() && frase3.length() < frase2.length()){
                    frases[1] = frase3;
                }
            }
        }
        System.out.println(frases[1]);

    }
}

I’m all day trying to do, I don’t know how a seemingly easy exercise is taking me so long

  • The first way is very simple, just compare the list strings with Comparator.

1 answer

5


The first form you used is the simplest, however, the Collections.Sort() used with Comparator String pattern will only arrange by alphabetical order.

In order to organize by string size, you need to create a Comparator for the method to sort() learn how to organize the list.

The form below, withdrawn of this reply in Soen, calf Comparator which returns the subtraction of the two strings once they are being compared:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JOptionPane;

public class App {

    public static void main(String[] args) {


        ArrayList<String> lista = new ArrayList<String>();
        lista.add(JOptionPane.showInputDialog("Frase 1:"));
        lista.add(JOptionPane.showInputDialog("Frase 2:"));
        lista.add(JOptionPane.showInputDialog("Frase 3:"));

        Collections.sort(lista, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                // TODO Auto-generated method stub
                return o1.length() - o2.length();
            }
        });            

        for(int i = 0; i < lista.size(); i++){
            System.out.print(lista.get(i));
            System.out.println(" - tamanho: " + lista.get(i).length());

        }

    }
}

The operation is simple: if the subtraction result is 0, it means that both strings have the same size, and in this case, the sort() will do nothing. If the result is greater than 0, it means that the first string is larger than the second string, so the sort() will swap them. If the result is less than zero, it means that the first string is less than the second, in this case the sort() probably won’t do anything either.

To illustrate how the code works, see the test below. I switched the dialog boxes for only text input and recommend you not mix graphical interface with text:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class App {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        ArrayList<String> lista = new ArrayList<String>();
        lista.add("frase que pode ser considerada muito longa");
        lista.add("frase curta");
        lista.add("frase com mais palavras");

        Collections.sort(lista, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                // TODO Auto-generated method stub
                return o1.length() - o2.length();
            }
        });

        for (int i = 0; i < lista.size(); i++) {
            System.out.print(lista.get(i));
            System.out.println(" - tamanho: " + lista.get(i).length());

        }
    }
}

See working on ideone

  • thank you very much, I learned a lot from your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.