0

I want to exit the do while loop when the user press the enter button, but I can't exit the do while loop.

The program asks for two words (pal1 and pal2) and shows which of them is the shortest one and show shows how many characters it has. If the words have the same length it orders them alphabetically. It also shows which of the words asked in the loop is the shortest one and should leave the loop when the string is void or null.

import java.util.Locale;
import java.util.Scanner;

/**
* The program asks for two words (pal1 and pal2) and shows which of them is the shortest     one and show shows how many characters it has. If the words have the same length it orders     them alphabetically. It also shows which of the words asked in the loop is the shortest one and should leave the loop when the string is void or null.
* 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class P5
{
 public static void main (String[] args){
    Scanner tec = new Scanner (System.in).useLocale(Locale.US);
    String pal1, pal2, palMasCorta="", palMasCortaAux;
    int longitudpal1, longitudpal2, longitudPalMasCorta=2000000000, longitudPalMasCortaAux;

    do{
        System.out.println("Escribe una palabra");
        pal1 = tec.next();
        longitudpal1= pal1.length();
        System.out.println("Escribe otra palabra");
        pal2 = tec.next();
        longitudpal2=pal2.length();
        palMasCortaAux=palMasCorta;
        longitudPalMasCortaAux=longitudPalMasCorta;
        if (longitudPalMasCorta>longitudPalMasCortaAux)
            palMasCorta=palMasCortaAux;
        if (longitudpal1>longitudpal2){
            palMasCorta = pal2;
            longitudPalMasCorta = longitudpal2;
            System.out.println("El número de caracteres de "+pal2+ " es "+longitudpal2);
        }
        else
            if (longitudpal1 == longitudpal2){

                if (pal1.compareTo(pal2) < 0){
                    System.out.println("El número de caracteres de "+pal1+ " es "+longitudpal1);
                    palMasCorta=pal1;
                }
                    else{
                        if (pal1.compareTo(pal2) > 0){
                            System.out.println("El número de caracteres de "+pal2+ " es "+longitudpal2);
                            palMasCorta=pal2;
                        }
                        else{
                            System.out.println("Has escrito la misma palabra, "+pal1+", y su longitud es " +longitudpal1);
                            palMasCorta=pal1;
                        }
                }
        }
            else{
                palMasCorta = pal1;
                longitudPalMasCorta = longitudpal1;
                System.out.println("El número de caracteres de "+pal1+ " es "+longitudpal1);
            }

            }while (pal1.length() == 0 || pal2.length() == 0);
    System.out.println("La palabra más corta es "+palMasCorta);
}

}

DarAR92
  • 1
  • 1
  • 1

6 Answers6

0

You are never setting pal1 or pal2 to null. Also you are not checking for null.

pal1 = null;
pal2 = null;

while(pal1 != null && pal2 != null)

Something like this is what you want. Just set pal1 or pal2 to null when you want to leave the loop. Also I don't see a need for a loop here unless the user is entering multiple words and you are checking multiple pairs of words...

brso05
  • 13,142
  • 2
  • 21
  • 40
0

you can write a message at startup like:

"Enter [q] to stop " or something liek this

And in program to comapre entered text with q

You can reformualte all with

String pal1="";

String pal2="";

while (pal1.equalsIgnoreCase("q") == false && pal2.equalsIgnoreCase("q") == false) {

///do all here etc

}

........ just a ideea

or you can do yours until pal1.isEmpty() or pal2.isEmpty()

CGeorgian
  • 551
  • 6
  • 10
0

You can add another if statement that says:

if(pal1==null && pal2==null){
   break;
}
jordaniac89
  • 574
  • 2
  • 8
  • 27
0

Instead of using do...while, I'd use an infinite while loop, and use break to break out of it when one of the words is null or empty (void in java means something else than an empty string).

while( true ) {

    System.out.println("Escribe una palabra");
    pal1 = tec.next();
    if ( pal1 == null || pal1.equals( "" ) ) {
        break;
    }

    System.out.println("Escribe otra palabra");
    pal2 = tec.next();
    if ( pal2 == null || pal2.equals( "" ) ) {
        break;
    }

    // Calculate the length only after you know the strings are not null or empty!

    // The rest of your code...
 }

The condition in your while loop was not good anyway because it did not check for null before calling a method. If the string is null, calling a method would cause a NullPointerException to be thrown.

Also, as other answers recommended, use a BufferedReader. If the user happens to enter the end-of-file character (ctrl-D or ctrl-Z), a Scanner will throw an exception on getNext(), while a BufferedReader will return null, which you are now checking for.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
0

Don't use a Scanner, use a BufferedReader for reading StdIn - you don't need a scanner here.

Scanner vs. BufferedReader

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null && s.length() != 0){
    System.out.println(s);
}

This will exit the loop when nothing is entered. (Just the Enter key)

Community
  • 1
  • 1
bwa-
  • 139
  • 1
  • 8
0

Change your next() calls to nextLine()

    ...
pal1 = tec.nextLine();
longitudpal1 = pal1.length();
System.out.println("Escribe otra palabra");
pal2 = tec.nextLine();
...

and then change the while test to look for empty string:

    } while (!pal1.isEmpty() && !pal2.isEmpty());
Kennet
  • 5,736
  • 2
  • 25
  • 24