This is what I'm trying to do:Add code that obtains the text from the text box, calls your method from the name program, and displays a message in the output label.
Here's are my methods from my name program:
public static void main(String[]args) { Scanner scan = new Scanner(System.in); String name = "Billy Bob"; boolean blank = false; while(!blank) //Prompt the user for a name and print it out { //Do this until a space is entered System.out.println("Please enter a name: "); name = scan.nextLine(); //Get the Name from the User if(!(name.length() == 0)) //If not blank, convert and print Name { name = convertName(name); System.out.println(name); blank = false; } else { blank = true; } //Exit the loop if it is a blank } } public static boolean hasComma(String tempName) //Return true if there is a comma in the name { int nameLength = tempName.length(); //Get length of the Name boolean comma = false; //Return value: True if there is comma for(int i=0; i<nameLength; i++) { if(tempName.charAt(i) == ',') { comma = true; } } return comma; } public static String convertName(String tempName) //Return the name in LastName, FirstName format { String firstName = "Billy"; //Need these to switch format String lastName = "Bob"; int indexOfSpace = 0; if(!hasComma(tempName)) //If there is no comma, switch format { indexOfSpace = tempName.indexOf(' '); //Find the first space between last and first name firstName = tempName.substring(0, indexOfSpace); //Set the first name from beginning till the space lastName = tempName.substring((indexOfSpace+1), tempName.length()); //Set the last name from the beginning of the second part of name till the end tempName = lastName + ", " + firstName; //Change format } return tempName; }
Here's my swing applet code that has problems:
public static boolean hasComma(String tempName) //Return true if there is a comma in the name { int nameLength = tempName.length(); //Get length of the Name boolean comma = false; //Return value: True if there is comma for(int i=0; i<nameLength; i++) { if(tempName.charAt(i) == ',') { comma = true; } } return comma; } public static boolean convertName(String tempName) //Return the name in LastName, FirstName format { boolean hasComma = hasComma(tempName); if(hasComma) { return hasComma; } else{ String firstName; //Need these to switch format String lastName; int indexOfSpace = 0; indexOfSpace = tempName.indexOf(' '); //Find the first space between last and first name firstName = tempName.substring(0, indexOfSpace); //Set the first name from beginning till the space lastName = tempName.substring((indexOfSpace+1), tempName.length()); //Set the last name from the beginning of the second part of name till the end tempName = lastName + ", " + firstName; //Change format boolean newName; newName = false; return newName; } } private void NameButtonCheckerActionPerformed(java.awt.event.ActionEvent evt) { String newName; newName = NameTextfield.getText(); boolean convertName = convertName(newName);//this is where the error is if (convertName) { System.out.println("Your name is in standard form."); } else { System.out.println("Your name has been converted."); } } private void NameTextfieldActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NameJApplet().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton NameButtonChecker; ///this is an error that says illegal start of an expression..how can I fix this? private javax.swing.JLabel NameLabel; private javax.swing.JTextField NameTextfield; private javax.swing.JLabel OutputLabel; // End of variables declaration }
I have been having problems trying to get the convertName to become a boolean.