I don\'t understand why when l enter a name less than two lettersthe program doe
ID: 3614977 • Letter: I
Question
I don't understand why when l enter a name less than two lettersthe program does not prompt me to enter another name because theone l entered is invalid. Where is my mistake ?
import javax.swing.*;
public class Methods
{
public static void main ( String [] args)
{ // opens main method
String cashiersName=JOptionPane.showInputDialog("Whats your name") ;
System.out.println(cashiersName);
} // closes main method
public static String getCashiersName( String cashiersName)
{ // opens method getCashiersName
String input=JOptionPane.showInputDialog(cashiersName);
int length = input.length();
int num=2;
while ( length<num) // open a whileloop for input validation of cashiers name
{
JOptionPane.showMessageDialog(null,"Enter a name more than twocharacters long");
System.out.println("Youentered " +" " + input +" "+ " that name isinvalid because it is less than two characters long");
input =JOptionPane.showInputDialog("Whats your name");
length = input.length();
} // close while loop
return input;
} //close methodgetCashierName
}
Explanation / Answer
import javax.swing.*; public class methods { public static void main ( String [] args) { // opens main method String cashiersName=JOptionPane.showInputDialog("Whats your name") ; String name = getCashiersName(); System.out.println(cashiersName); } // closes main method public static void String getCashiersName() //remove void ..as it has to returnString { // opens method getCashiersName String input =JOptionPane.showInputDialog("What's yourname: "); int length = input.length(); int num=2; while ( lengthRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.