Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: swing applet for name problems

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default swing applet for name problems

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swing applet for name problems

    having problems trying to get the convertName to become a boolean
    Can you explain what the problem is?
    When should it return true and when false?

    The comment next to the method does not correspond to what the method returns.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: swing applet for name problems

    Quote Originally Posted by Norm View Post
    Can you explain what the problem is?
    When should it return true and when false?

    The comment next to the method does not correspond to what the method returns.
    A problem is that the line private javax.swing.JButton NameButtonChecker; is saying it is an illegal start of an expression. Also, the output is supposed to be the name that is entered in the text as a name in standard form. For example, one enters billy joe. The output would be Joe, Billy. so last and first with a comma between

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swing applet for name problems

    saying it is an illegal start of an expression.
    If you are getting compiler errors, please copy the full text of the error message and paste it here.

    the output is supposed to be the name that is entered in the text as a name in standard form.
    The output of what? Where is the output done?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with Swing applet
    By IHeartProgramming in forum AWT / Java Swing
    Replies: 3
    Last Post: December 23rd, 2012, 02:53 AM
  2. Swing (Problems)
    By LoganC in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 22nd, 2012, 03:48 PM
  3. HTML in Swing Components in Cached Applet
    By KevinWorkman in forum Java Applets
    Replies: 6
    Last Post: August 1st, 2011, 07:46 AM
  4. Replies: 0
    Last Post: November 27th, 2010, 10:47 PM
  5. applet + swing + arraylist = exception??
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2010, 11:38 PM