Results 1 to 4 of 4
Thread: Compare not working
- 02-13-2011, 01:32 AM #1
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" }; }
- 02-13-2011, 03:29 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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 + "|"); }
- 02-13-2011, 11:57 AM #3
Thanks. I forgot I put in the extra code to find another error.
- 02-13-2011, 05:49 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
\n not working in GUI (working code, but \n isn't working)
By cc11rocks in forum New To JavaReplies: 2Last Post: 01-04-2011, 04:30 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Compare 5 numbers
By Snowboardmylife in forum New To JavaReplies: 5Last Post: 04-15-2008, 07:04 PM -
Compare 2 XML
By Peter in forum XMLReplies: 1Last Post: 07-05-2007, 02:58 AM -
String Compare not working
By Revelation in forum New To JavaReplies: 3Last Post: 06-30-2007, 06:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks