Results 1 to 4 of 4
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default Compare not working

    For some reason the string compare in findMessage is not working, the routine always returns the "Code not found" message (only code right now is 0326). I cannot see what is wrong with the code.

    Java Code:
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.io.*;
    import java.lang.Integer;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.border.BevelBorder;
    
    
    /**
    	*---------------------------------------------------------------------*
    	*   Mainframe Errors                                                  *
    	*   @author  Mike Lipay                                               *
    	*   @date    02/10/2011   @version 1.0                                *
    	*---------------------------------------------------------------------*
    */
    public class MFErrors
    {
    	public static void main (String args[])
    	{
    		displayMenu();
    	}
    
    
    	/**
    		*------------------------------*
    		*   Display the Error window   *
    		*------------------------------*
    	*/
    	public static void displayMenu()
    	{
    		EventQueue.invokeLater (new Runnable()
    		{
    			public void run()
    			{
    				MainFrame frame = new MainFrame();
    				frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			}
    		});
    	}
    }
    
    
    
    /**
    	*-----------------------------*
    	*   Create the window frame   *
    	*-----------------------------*
    */
    class MainFrame extends JFrame
    {
    	public MainFrame()
    	{
    // Main window setup
    	setLocationByPlatform(true);
    	setSize(windowWidth, windowHeight);
    	setTitle("Mainframe Error Messages  PI Software");
    
    	JPanel selectPanel = new JPanel();
    	selectPanel.setLayout(new GridLayout(4,2));
    
    
    // add the Systems (IDMS, CICS, etc.)
    	for ( String system : systems )	systemCombo.addItem(system);
    	selectPanel.add(new JLabel (" Systems: "));
    	selectPanel.add(systemCombo);
    
    
    // add the Code input
    	selectPanel.add(new JLabel (" Error Code: "));
    	codeField.setText("");
    	selectPanel.add(codeField);
    
    
    // add the Find button
    	selectPanel.add(new JLabel (" "));
    	JButton findButton = new JButton("Find");
    	findButton.addActionListener(new FindAction());
    	selectPanel.add(findButton);
    
    
    // add the Error Message area
    	selectPanel.add(new JLabel (" Error Message: "));
    	messageArea.setText(findMessage(" ", "    "));
    	messageArea.setEditable(false);
    	messageArea.setLineWrap(true);
    	messageArea.setWrapStyleWord(true);
    	messageArea.setBorder(BorderFactory.createEtchedBorder());
    	selectPanel.add(messageArea);
    
    
    // add the panel to the frame
    	add(selectPanel, BorderLayout.NORTH);
    	}
    
    
    	/**
    		*---------------------------*
    		*   Application Variables   *
    		*---------------------------*
    	*/
    	static int windowWidth  = 500;
    	static int windowHeight = 300;
    	static String spacerText = "    ";
    
    	// System arrays
    	String[] systems	= { "CICS", "IDMS", };
    
    	// Main panel
    	private JPanel mfErrorPanel     = new JPanel();
    
    	// Combo box fields
    	private JComboBox systemCombo  = new JComboBox();
    	private JTextField codeField   = new JTextField(5);
    	private JTextArea  messageArea = new JTextArea();
    
    
    	private class FindAction implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    		String system = systemCombo.getSelectedItem().toString();
    		String code   = codeField.getText();
    		messageArea.setText(findMessage(system, code));
    		}
    	}
    
    
    	public String findMessage (String system, String code)
    	{
    		int codeIdx;
    		if (code.length() != 4) return "Code must be 4 characters";
    		
    		String temp = code.length() + "." + code.substring(0,4);
    		code = temp;
    
    		for ( codeIdx = 0; codeIdx < codeArray.length; codeIdx++)
    		{
    			temp = codeArray[codeIdx];
    			if (temp.equalsIgnoreCase(code))	return messageArray[codeIdx];
    		}
    		return ("Code not found:" + code + temp + "|");
    	}
    
    	// Error code arrays
    	String[] codeArray = {"0326", "END*" };
    	String[] messageArray = { "Record not found", "Code not found" };
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    the routine always returns the "Code not found" message

    What is the actual output?

    ---------------------------

    Try using System.out.println() to see what you are searching for and what you are comparing with:

    Java Code:
    public String findMessage (String system, String code)
    {
        int codeIdx;
        if (code.length() != 4) return "Code must be 4 characters";
    		
        String temp = code.length() + "." + code.substring(0,4);
        code = temp;
    
        [b]System.out.println("Looking for " + code)[/b]
        for ( codeIdx = 0; codeIdx < codeArray.length; codeIdx++)
        {
            [b]System.out.println("    comparing with " + codeArray[codeIdx]);[/b]
            temp = codeArray[codeIdx];
            if (temp.equalsIgnoreCase(code))	return messageArray[codeIdx];
        }
        return ("Code not found:" + code + temp + "|");
    }

  3. #3
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Thanks. I forgot I put in the extra code to find another error.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2011, 05:30 AM
  2. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  3. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 08:04 PM
  4. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 03:58 AM
  5. String Compare not working
    By Revelation in forum New To Java
    Replies: 3
    Last Post: 06-30-2007, 07:43 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
  •