[英]Java if statement won't run - Testing to see if a substr(0,1) is == 00
我有一个随机访问文件,它以"00, , "
格式保存记录-我想做的是从随机访问文件中读取内容,并检查是否在特定位置存在条目(如代码中所写)下方)。 读取记录很好,但是当它读取带有substr(0, 1)
的记录时substr(0, 1)
它仍将输出text field GUIS which would normally hold the values I need (Date, Hour, and Client Name)
该text field GUIS which would normally hold the values I need (Date, Hour, and Client Name)
。 我尝试将substr
更改为values[0]
,这是一个数组,用于拆分变量以保存他们搜索的特定position
的值。 s - This is the variaable that holds the line
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;
}
}
解决此问题的原因只是更改了
if (s.substring(0,1).equals("00")) {
noResult.setText("No Results Found");
}
TO-我将substr(0,1)更改为(0,2)
if (s.substring(0,2).equals("00")) {
noResult.setText("No Results Found");
}
并将noResult的GUI add(noResult)
到JFRAME add(noResult)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.