Results 1 to 2 of 2
  1. #1
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    23
    Rep Power
    0

    Default Jtextfield cannot be converted to string

    For some reason, i can't .getText(); and put it into a string variable named postal.

    Java Code:
    package customerlist;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import javax.swing.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    /**
     *
     * @author 358512
     */
    public class getInput{
        int listCount = 0;
        String names = "";
        String cities = "";
        String addresses = "";
        String provinces= "";
        String postalCodes = "";
        
        public void userInput(){
            JFrame main = new JFrame();
            main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  
            final JTextField name = new JTextField(20);
            final JTextField city = new JTextField(10);
            final JTextField address = new JTextField(20);
            final JTextField province = new JTextField(10);
            final JTextField postalCode = new JTextField(10);
            JButton addNew = new JButton("Add Customer");
            JButton done = new JButton("Done");
            
            
            done.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    try {
                        checkPostalCode(postalCode);
                        
                        File customerList = new File("H:\\customerList.txt");
                        FileWriter fileWriter = new FileWriter(customerList, true);
                        BufferedWriter buffer = new BufferedWriter(fileWriter);
                        PrintWriter fileOut = new PrintWriter(buffer);
                        
                        String postal = postalCode.getText();
                        
                        boolean error = checkPostalCode(postal);
                        
                        if(error == false){
                            JOptionPane.showMessageDialog(null, "Your postal code was in incorrect format.\n\n"
                                    + "Please enter information again.", "Error", JOptionPane.ERROR_MESSAGE);
                            userInput();
                        }
                        
                        fileOut.println("Name: " + name.getText());
                        fileOut.println("City: " + city.getText());
                        fileOut.println("Address: " + address.getText());
                        fileOut.println("Province: " + province.getText());
                        fileOut.println("Postal Code: " + postalCode.getText());
                        fileOut.println("------------------------------------\n\n");
                        
                        fileOut.close();
                        System.exit(0);
                    } catch (IOException ex) {
                        Logger.getLogger(getInput.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }    
            });
            
            addNew.addActionListener(new ActionListener(){
    
                public void actionPerformed(ActionEvent e){
                    try {
                        File customerList = new File("H:\\customerList.txt");
                        FileWriter fileWriter = new FileWriter(customerList, true);
                        BufferedWriter buffer = new BufferedWriter(fileWriter);
                        PrintWriter fileOut = new PrintWriter(buffer);
                           
                        fileOut.println("Name: " + name.getText());
                        fileOut.println("City: " + city.getText());
                        fileOut.println("Address: " + address.getText());
                        fileOut.println("Province: " + province.getText());
                        fileOut.println("Postal Code: " + postalCode.getText());
                        fileOut.println("------------------------------------\n\n");
                        
                        fileOut.close();
                        
                        name.setText("");
                        city.setText("");
                        address.setText("");
                        province.setText("");
                        postalCode.setText("");
                        userInput();
                    } catch (IOException ex) {
                        Logger.getLogger(getInput.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            
    
            
            main.setLayout(new GridLayout(0,1));
            JPanel customerInput = new JPanel(new GridLayout(0,1));
            
            main.add(customerInput);
            customerInput.add(new JLabel("Name:"));
            customerInput.add(name);
            customerInput.add(new JLabel("City:"));
            customerInput.add(city);
            customerInput.add(new JLabel("Address:"));
            customerInput.add(address);
            customerInput.add(new JLabel("Province:"));
            customerInput.add(province);
            customerInput.add(new JLabel("Postal Code:"));
            customerInput.add(postalCode);
            customerInput.add(addNew);
            customerInput.add(done);
            
            customerInput.setVisible(true);
            
             
            
            JOptionPane.showMessageDialog(null, customerInput, "Customer List", JOptionPane.INFORMATION_MESSAGE);
        }
        
        public boolean checkPostalCode(String postalCode){
            boolean error = true;
            if(Character.isLetter(postalCode.charAt(0))){
                if(Character.isDigit(postalCode.charAt(1))){
                    if(Character.isLetter(postalCode.charAt(2))){
                        if(postalCode.charAt(3) == 32){
                            if(Character.isDigit(postalCode.charAt(4))){
                                if(Character.isLetter(postalCode.charAt(5))){
                                    if(Character.isDigit(postalCode.charAt(6))){
                                        postalCode = postalCode.toUpperCase();
                                        error = false;
                                        return error;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return error;
        
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Jtextfield cannot be converted to string

    Line 39: You are passing a JTextField to checkPostalCode when it expects a String.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. how to delete single string from JTextField??
    By raffs03 in forum New To Java
    Replies: 2
    Last Post: 07-03-2013, 09:35 AM
  2. Help with separating a String to be converted to a Char
    By AnnaPatricia in forum New To Java
    Replies: 10
    Last Post: 08-16-2012, 05:49 PM
  3. Replies: 2
    Last Post: 07-05-2012, 08:10 AM
  4. Hejp with deleting string from JTextField
    By satin in forum New To Java
    Replies: 9
    Last Post: 11-08-2008, 11:05 PM
  5. Replies: 4
    Last Post: 03-25-2008, 12:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •