简体   繁体   中英

Java if statement won't run - Testing to see if a substr(0,1) is == 00

I have a Random Access file that holds records in this format "00, , " - What I'm trying to do is read from the random access file and check to see if there are entries at a particular position ( As written in the code below ). Reading the records is fine but when It reads a record with a substr(0, 1) it will still output the text field GUIS which would normally hold the values I need (Date, Hour, and Client Name) . I've tried changing substr to values[0] which is an array that splits up the variable holds the value of the particular position they searched for. s - This is the variaable that holds the line

My Java FILE - fileWriter.txt the randomAccess file

public class readRandomDataFile extends JFrame implements ActionListener{

    private static Path file = Paths.get("fileWriter.txt");
    private static String s = "00,          ,  "
            + System.getProperty("line.separator");
    private static FileChannel fc = null;
    private static int RECSIZE = s.length();



    private static JButton submit;
    private static JLabel output;
    private static JLabel bankDetails;
//    private static JTextField txtClient = new JTextField("", 20);
    private static JTextField txtDate = new JTextField("",6);
    private static JTextField txtClient = new JTextField("",15);
    private static JTextField txtHour = new JTextField("",6);


//    private static JTextField txtHour = new JTextField("",6);

    private static JLabel lblClient = new JLabel("Client");
    private static JLabel lblDate = new JLabel("Date");
    private static JLabel lblHour = new JLabel("Hour");
    private static JLabel noResult = new JLabel("");
    private static JLabel lblHeading1 = new JLabel("<html><h1>Meeting Calendar</h1></html>");
    private static JLabel lblHeading2 = new JLabel("<html><h2>Enter this months appointment</h2></html>");

    private int date;


   public readRandomDataFile(){
        super("Read Date File");
        setSize(300,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        output = new JLabel();
        submit = new JButton("Submit");

        bankDetails = new JLabel();
        setLayout(new FlowLayout());

        add(lblHeading1);
        add(lblHeading2);

//        add(lblDate);
//        add(txtDate);
//        txtClient.addActionListener(this);

        add(lblDate);
        add(txtDate);
        txtDate.addActionListener(this);

//        add(lblHour);
//        add(txtHour);
//        txtHour.addActionListener(this);


        add(submit);
        submit.addActionListener(this);

        add(output);


    } 
    public void actionPerformed(ActionEvent e){
        date = Integer.parseInt(txtDate.getText());
        Object source = e.getSource();

        String strID = "";

        if(source == submit){
            byte[] data = s.getBytes();
            ByteBuffer buffer = ByteBuffer.wrap(data);
            try{
                fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
                strID = txtDate.getText();
                    buffer = ByteBuffer.wrap(data);
                    fc.position(date * RECSIZE);
                    fc.read(buffer);

                    s = new String(data);

                    String[] values = s.split(",", -1);
                    if(s.substring(0,1).equals(00)){
                        noResult.setText("No Results Found");
                    }
                    else{
                            output.setText(values[0]);
                            output.setVisible(false);
                            add(lblClient);
                            add(txtClient);
                            add(lblHour);
                            add(txtHour);
                            txtClient.setText(values[1]);
                            txtHour.setText(values[2]);
                       //output.setText("Info for ");
                    }
                fc.close();            
            }
            catch(IOException er){
                System.out.println("Error connecting to or reading to file");
            }
        }
        }
    public static void main(String[] args){
        readRandomDataFile one = new readRandomDataFile();
        one.setVisible(true);

//        int id;
//        String strID = "";
//        double fee;

        }

       }

What fixed this was simply changing the

 if (s.substring(0,1).equals("00")) {
     noResult.setText("No Results Found");
 }

TO - I changed the substr(0,1) to (0,2)

if (s.substring(0,2).equals("00")) {
    noResult.setText("No Results Found");
}

and also added the GUI for noResult to the JFRAME add(noResult)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM