Results 1 to 2 of 2
  1. #1
    nanderson05 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default School program GUI output problem

    Hello, I am making a program for one of my classes. The program is working as intended but I think that the problem area is the output. The program is designed to accept user input for a sentence then an additional text box to accept a letter or a number that it will count how many occur in the first input of the sentence. However no matter what I do I get an output of this:

    Number of javax.swing.JTextField[,826,35,15x20,layout=javax.swing.plaf.basic.BasicT extUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0, border=javax.swing.plaf.BorderUIResource$CompoundB orderUIResource@1d5a0,flags=296,maximumSize=,minim umSize=,preferredSize=,caretColor=sun.swing.PrintC olorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResourc e[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIRes ource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=1,columnWidth=11,command=,horizontalAlign ment=LEADING]'s: 9

    Java Code:
    	results.setText("Number of " + searchText + "'s: " 
    			+ count);
    as you can see that is the code that is trying to execute. It does the "Number of" and the count section, as you can see the last part of the block of output haha. But the searchText isn't really working. Is it because I need to make a new string variable that will = the string that is the input for the searchText? Or what would you recommend?

    Here is my entire code:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.JFrame;
    
    
    /***********************************************************
    Program Name: Index
    Programmers Name: Nicholas Anderson
    Program Description: This program checks the occurrences of characters, etc...
    ************************************************************/
    public class indexClass extends JFrame
    {
    private JFrame mainFrame;
    private JButton searchButton;
    private JLabel mainLabel;
    private JLabel searchLabel;
    private JTextArea mainText;
    private JTextField searchText;
    private JPanel panel;
    private JTextField results;
    
    public String userMainText, userSearchText;
    public int current, count;
    public char key;
    
    
    public indexClass() {
    
    mainFrame = new JFrame("Index Search");
    searchButton = new JButton("Search the letter");
    mainLabel = new JLabel("Enter text to be searched:");
    searchLabel = new JLabel("Enter a letter or character:");
    searchText = new JTextField(1);
    mainText = new JTextArea(5, 10);
    panel = new JPanel();
    results = new JTextField(15);
    
    
    
    mainFrame.addWindowListener(new WindowAdapter() {
    	public void windowClosing(WindowEvent e) {
    	System.exit(0);
    	}
    	});
    
    SearchButtonHandler sbHandler = new SearchButtonHandler();
    searchButton.addActionListener(sbHandler);
    
    searchButton.setMnemonic('S');
    
    Container c = mainFrame.getContentPane();
    
    c.setLayout(new BorderLayout());
    panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    
    panel.add(mainLabel);
    panel.add(mainText);
    panel.add(searchLabel);
    panel.add(searchText);
    panel.add(searchButton);
    panel.add(results);
    
    c.add(panel);
    
    mainFrame.setSize(400, 300);
    mainFrame.setVisible(true);
    }
    
    
    
    class SearchButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent arg0)
    {
    	
    	String userMainText, userSearchText;
    	count = 0;
    	char key;
    	
    	userMainText = mainText.getText().toLowerCase();
    	userSearchText = searchText.getText().toLowerCase();
    	
    	
    	key = userSearchText.charAt(0);
    	
    	current = userMainText.indexOf(key);
    	
    	while (current != -1)
    	{
    	count++;
    	current = userMainText.indexOf(key, current+1);
    	}
    	
    	results.setText("Number of " + searchText + "'s: " 
    			+ count);
    	
    }
    }
    public static void main(String[] args) {
    	new indexClass();
    }
    }
    Thank you any help would be appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: School program GUI output problem

    searchText is working fine. What you're trying to do is print out the toString() method of a JTextField, and as you're finding out, that's not what you really want to do. Instead you should extract the text that the JTextField holds using its getText() method, and then print out that. Most important, please read the JTextField tutorial as this is all well spelled out there.

Similar Threads

  1. Date printing program school project
    By nanderson05 in forum New To Java
    Replies: 2
    Last Post: 05-21-2012, 04:55 AM
  2. Payroll program.. School.. Help Please!
    By eddyb5 in forum New To Java
    Replies: 2
    Last Post: 06-05-2011, 05:49 PM
  3. Replies: 3
    Last Post: 09-26-2010, 04:21 AM
  4. School project: network program via java any ideas?
    By Sernomicus in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 03:46 PM
  5. Beginner Needs Help w/ Program for School
    By badness in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:51 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
  •