public button is always set to false - java

here's my problem in java, my button is set on public because it is on different window and now i put a function to this button but when I always open the window that the button is included the button is always set to false even the button is clicked it is not functioning.
BTW
veiwTable is a new window:(maybe somebody will laugh to my spelling but I intentionally set it to wrong due to my other variables :) )
convertToTxt is a button
I input else to check if the function is set to false when opening the window
here is my code:
if(veiwTable.convertToTxt.isSelected()) {
try{
File file = new File("e:\\Data Logs\\ " + sn + "_" + status + ".txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Board Name: " + boardName);
bw.newLine();
bw.write("Part Number: " + pn);
bw.newLine();
bw.write("Serial Number: " + sn);
bw.newLine();
bw.write("Board Revision: " + bRev);
bw.newLine();
bw.write("Failing Test Parameter: " + failingTest);
bw.newLine();
bw.write("Failing Checker: " + checker);
bw.newLine();
bw.write("Verified By: " + verifiedBy);
bw.newLine();
bw.write("Remakrs: " + remarks);
bw.newLine();
bw.write("Tester Number: " + testerNumber);
bw.newLine();
bw.write("Datalog:");
bw.newLine();
bw.write(Datalogs );
bw.close();
String note = boardName.concat(" with ").concat(sn).concat(" is located on 'ETS88-spare'\'E:'\'Data Logs'"); //" with " + sn " is located on 'EData Logs'"
JOptionPane.showMessageDialog(null, note);
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
} else JOptionPane.showMessageDialog(null, "none");

update: my program is now running properly after converting to string all the data on the text box and call it afterwards.
private void convertToTxtActionPerformed(java.awt.event.ActionEvent evt) {
//////not included on the generated datalogs///
String lastDevice = jLastDevice.getText();
String progname = jProgramName.getText();
String progRev = jProgramRevision.getText();
/////////////////////////////////////////////
String boardname = jBoardname.getText();
String pn = jPN.getText();
String sn = jSN.getText();
String boardrev = jBoardRev.getText();
String verifStatus = jVerificationStatus.getText();
String failedTNum = jFailedTNum.getText();
String checker = jFailingChecker.getText();
String tester = jTesterNumber.getText();
String remarks = jRemarks.getText();
String verifiedBy = jVerifiedBy.getText();
String dLogs = jDatalog.getText();
try{
File file = new File("\\\\192.168.1.100\\e\\Data Logs\\ " + sn + "_" + verifStatus + ".txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Board Name : " + boardname);
bw.newLine();
bw.write("Part Number : " + pn);
bw.newLine();
bw.write("Serial Number : " + sn);
bw.newLine();
bw.write("Board Revision : " + boardrev);
bw.newLine();
bw.write("Failing Test Parameter : " + failedTNum);
bw.newLine();
bw.write("Failing Checker : " + checker);
bw.newLine();
bw.write("Verified By : " + verifiedBy);
bw.newLine();
bw.write("Remakrs : " + remarks);
bw.newLine();
bw.write("Tester Number : " + tester);
bw.newLine();
bw.write("Datalog");
bw.newLine();
bw.newLine();
bw.write(dLogs );
bw.close();
String note = boardname.concat(" with ").concat(sn).concat(" is located on 'ETS88-spare'\'E:'\'Data Logs'"); //" with " + sn " is located on 'EData Logs'"
JOptionPane.showMessageDialog(null, note);
//System.out.println(note);
}catch(IOException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
}
}

Related

How do I read data from file (with specific condition) and display the information on the screen

vaccination.txt
Can someone help me with my codes? I have a problem displaying the information on the screen. I need to display the info based on the question requirement below:
Display all information about those born in Selangor who received the booster dose (dose 3) on screen. The person born in Selangor is represented by two digits there are 10 after the six digits that represent the birth date from the identification (ic) number.
I also have a problem writing the data into the relevant files. I tried the same method (by using the print writer) as what I have done before, but it doesn't work here and Idk why. Do help me
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String\[\] args) {
File inFile = new File ("vaccination.txt");
File comorbid = new File ("comorbid.txt");
File nonComorbid = new File ("non_comorbid.txt");
try {
//read data from file
Scanner sc = new Scanner (inFile);
//write data into file
PrintWriter pw = new PrintWriter(comorbid);
PrintWriter pw2 = new PrintWriter (nonComorbid);
String vaccinePlace = " ", ICnum = " ", category = " ", vaccineType = " ";
int doseNum = 0;
pw.println("Matric Name Part Gender");
pw.println("--------------------------------------------------------------------");
pw2.println("Matric Name Part Gender");
pw2.println("--------------------------------------------------------------------");
while(sc.hasNext()) //check line by line
{
String data = sc.nextLine();
StringTokenizer st = new StringTokenizer(data, ":");
vaccinePlace = st.nextToken();
ICnum = st.nextToken();
category = st.nextToken();
vaccineType = st.nextToken();
doseNum = Integer.parseInt(st.nextToken());
//display information on screen
if (doseNum == 3)
{
if (ICnum.substring(6,8).equalsIgnoreCase("10"))
{
System.out.println("Vaccine place: " + vaccinePlace);
System.out.println("IC number: " + ICnum);
System.out.println("Category: " + category);
System.out.println("Vaccine type: " + vaccineType);
System.out.println("Dose number: " + doseNum);
}
}
//write and store information into comorbid.txt and nonComorbid file
if (category.equalsIgnoreCase("comorbid")) {
pw.println(vaccinePlace + " " + ICnum + " " + vaccineType + " " + doseNum);
}
if (category.equalsIgnoreCase("non-comorbid")) {
pw.println(vaccinePlace + " " + ICnum + " " + vaccineType + " " + doseNum);
}
} //end loop
sc.close();
pw.close();
pw2.close();
}
catch (FileNotFoundException fnf) {
System.out.println(fnf.getMessage());
}
catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

How to loop writer .csv?

I want every data to be retrieved once looping will occur in the CSV writer, how do I make the loop occur?
thanks!
Data from Firebase
Output writer
*sorry, i can't display images 'cause it must have 10 reputations :(
try {
File root = new File(Environment.getExternalStorageDirectory() + "/StockCount/");
if (!root.exists()) {
root.mkdirs();
}
File myCSV = new File(root, currentDate + " DataStockCount.csv");
myCSV.createNewFile();
FileWriter writer = new FileWriter(myCSV);
writer.append("Date : " + getDate + "\n");
writer.append("Inspector : " + getInspector + "\n");
writer.append("Location : " + getLocation + "\n");
writer.append("Product Name : " + getProductName + "\n");
writer.append("Price : " + getPrice + "\n");
writer.append("Quantity : " + getAmount + "\n");
writer.append("Barcode : " + getCode + "\n");
writer.flush();
writer.close();
Toast.makeText(this, "File Saved Successfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Why not using an existing library like FastCSV?

Linux and java: File is being created but text is not written

I am writing a simple terminal program that logs some information, and puts it into a text file that someone can recall on later. Mainly just to have a log of what he person has done. I have been fine in windows, and have not really had this issue, but i fear i am looking over something simple.
Like I said before, if I navigate to the project directory, I see the file has been created, but when I open the file with the text editor, none of the data in the created string is printed.
private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,String city, String state) throws IOException{
File fileName = new File("log.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(fileName);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));
int i =0;
write.write(i +"; " + call + "; " + freq + "; " + mode + "; " + rstSnt + "; " + rstRx + "; " + city + "," + state + "\n");
i++;
System.out.println("File has been updated!");
} catch (FileNotFoundException ex) {
Logger.getLogger(QtLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
You need to close the output, or more correctly, you need to code so it will be closed (not necessarily closing it explicitly). Java 7 introduced the try with resources syntax that neatly handles exactly this situation.
Any object that is AutoCloseable can be automatically, and safely, closed using this syntax, like this:
private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,String city, String state) throws IOException{
File fileName = new File("log.txt");
try (FileOutputStream fos = = new FileOutputStream(fileName);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));) {
int i =0;
write.write(i +"; " + call + "; " + freq + "; " + mode + "; " + rstSnt + "; " + rstRx + "; " + city + "," + state + "\n");
i++;
System.out.println("File has been updated!");
} catch (FileNotFoundException ex) {
Logger.getLogger(QtLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
Just moving the initialization of your closable objects into the try resources block will ensure they are closed, which will flush() them as a consequence of being closed.
After calling the write() function from the BufferedWriter class, you need to call the close() function. You should also call the close() function on your FileOutputStream object.
So your new code should look like this:
private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,String city, String state) throws IOException{
File fileName = new File("log.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(fileName);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));
int i =0;
write.write(i +"; " + call + "; " + freq + "; " + mode + "; " + rstSnt + "; " + rstRx + "; " + city + "," + state + "\n");
// Close your Writer
write.close();
// Close your OutputStream
fos.close();
i++;
System.out.println("File has been updated!");
} catch (FileNotFoundException ex) {
Logger.getLogger(QtLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}

Why my method don't read all of my File? ... Java?

I have to display all of records in my blocks (text files), and do a split to "cover" the Fields Separators, but only the first record of my blocks are diplayed. What am I doing wrong?
enter code here
public static void listAllStudents() throws IOException {
File path = new File(Descriptor.getBlockPath());
for (int i = 0; i < path.listFiles().length; i++) {
try {
FileInputStream file = new FileInputStream(Descriptor.getBlockPath() + "BLK" + i + ".txt");
InputStreamReader entrada = new InputStreamReader(file);
BufferedReader buf= new BufferedReader(entrada);
String piece = " ";
System.out.println("\nBLOCO " + i + " ------------------------------------------------------ +");
do {
if (buf.ready()) {
piece = buf.readLine();
System.out.println("\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨");
String string = " ", field[] = piece.split(Descriptor.getFieldSeparator());
string = " ";
System.out.println("CPF: " + field[0]);
System.out.println("Name: " + field[1]);
System.out.println("Course: " + field[2]);
System.out.println("Age: " + field[3]);
System.out.println("Phone: " + field[4]);
System.out.println("Active: " + field[5]);
string = " ";
}
} while (buf.ready());
buf.close();
} catch (IOException e) {
System.out.println();
}
}
}
See the documentation for the BufferedReader.readLine() method:
or null if the end of the stream has been reached
Then change your code to read the file line by line:
while ((piece = buf.readLine()) != null) {
String field[] = piece.split(Descriptor.getFieldSeparator());
if (field.length >= 6) {
System.out.println("CPF: " + field[0]);
System.out.println("Name: " + field[1]);
System.out.println("Course: " + field[2]);
System.out.println("Age: " + field[3]);
System.out.println("Phone: " + field[4]);
System.out.println("Active: " + field[5]);
}
}

Writing to .txt skipping lines and leaving white spaces java

String newPurchaseOrder = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
try {
String filename = "PurchaseOrderDataFile.txt";
FileWriter fw = new FileWriter(filename, true); //the true will append the new data
BufferedWriter bw = new BufferedWriter(fw);
FileReader fr = new FileReader("PurchaseOrderDataFile.txt");
bw.write("\n" + newPurchaseOrder);
bw.close();
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
Trying to prevent it from skipping lines when inputting to the .txt file
08/12/13#PMI-1256#DT/9489#8
16/12/13#ENE-5789#PV/5732#25
27/12/13#PEA-4567#PV/5732#3#
09/01/14#PEA-4567#DT/9489#1
16/01/14#EMI-6329#PV/5732#8
16/07/13#ESE-5577#ZR/7413#6
Input skips lines such as above
what do you mean "skipping lines"? bw.write("\n" + newPurchaseOrder); will first put an empty line if that is what you mean, just transfer "\n" to the end.. The following code works fine:
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
class myWrite {
public static void main(String[] args) {
String dateStr = "test";
String customerID = "1";
String productCode = "100";
String qty = "1000";
String newPurchaseOrder = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
String newPurchaseOrder2 = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
try {
String filename = "PurchaseOrderDataFile.txt";
FileWriter fw = new FileWriter(filename, true); //the true will append the new data
BufferedWriter bw = new BufferedWriter(fw);
FileReader fr = new FileReader("PurchaseOrderDataFile.txt");
bw.write(newPurchaseOrder + "\n");
bw.write(newPurchaseOrder2 + "\n");
bw.close();
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
}
}
writes:
test#1#100#1000
test#1#100#1000
EDIT: Using the output you told me,
08/12/13#PMI-1256#DT/9489#8
16/12/13#ENE-5789#PV/5732#25
27/12/13#PEA-4567#PV/5732#3#
09/01/14#PEA-4567#DT/9489#1
16/01/14#EMI-6329#PV/5732#8
I then added the line you told me:
16/07/13#ESE-5577#ZR/7413#6
which produces:
08/12/13#PMI-1256#DT/9489#8
16/12/13#ENE-5789#PV/5732#25
27/12/13#PEA-4567#PV/5732#3#
09/01/14#PEA-4567#DT/9489#1
16/01/14#EMI-6329#PV/5732#8
16/07/13#ESE-5577#ZR/7413#6
use my code it works fine and does what you want..

Categories