I am trying to print the contents of a file that is read using a while loop after the user selects it from the JChooser prompt window. The file is written in a program and contains an ID, first name, last name, title, and year of birth that is stored in a written data file as "000001BilboBagginsEsq.1020" I am trying to use another program to choose the data file print the contents to the console such that it displays as:
000001 Bilbo Baggins Esq. 1020
I want to use the printf statement to format the data that is read and stored in the "rec" variable in this manner, but when I pass in the data from the file it seems to ignore the printf format and prints the data like this in the console:
1 000001BilboBagginsEsq.1020
My program uses the JChooser to pick a file and print its contents using a while loop:
while(reader.ready())
{
rec = reader.readLine();
line++;
System.out.printf("\n%4d %-60s ", line, rec);
}
reader.close();
System.out.println("\n\nData file read!");
Any help on this would be appreciated.
I tried formatting the rec using printf, expecting the output to display:
000001 Bilbo Baggins Esq. 1020
But instead it still prints:
1 000001BilboBagginsEsq.1020
Related
I've got a text file like this: Image
The first column represents the user ID and the last column represents balance. I want to iterate through the elements to find a specific user ID and then update their balance. I've managed to match the user ID and access the balance using the Scanner but I'm not sure how to update the balance in the text file with a new value. This is the code I've written so far:
File file = new File("UserInfo.txt");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
String info = sc.nextLine();
String data[] = info.split(" ");
//userId is a function argument
if(String.valueOf(userId).equals(data[0])){
//I want to update the balance in the text file with a new value here
}else{
continue;
}
}
sc.close();
it seems that you want to create a CSV file. My advice is to use ";" as a separator for the split() command, and if the file is not too big, read the whole file, put it into an arrayList of Strings and then manipulate It, rewriting the whole file at the end from the arrayList. Otherwise you have to use RandomAccess class
Replacing string in file is not so simple. You have to read whole file and rewrite part that you want to leave unchanged. Only when condition of comparing userID and data[0] is met, you would use replace() function to change user balance.
Your file is basically CSV file with space as line separator, as already mentioned store file contents in array or BufferedReader, change appropriate line and then export back to CSV.
I have a R script in which I want to call parameters from Java code. The parameters are csv file name file name and unique ID which has to be used to name the two output files.
My R script is :
df1 <- read.csv("filename.csv")
vs=colnames(df1)
md=formula(paste(vs[3],"~",vs[1],"+",vs[2]))
fit <- summary(aov(md, data=df1))[[1]]
#text output
names(fit)[1:4]=c("DF","SS","MS","F")
sink("test.txt")
In this code the first line df1 <- read.csv("filename.csv") should take file name dynamically from JAVA code and the last line sink("test.txt") should take unique ID and create the output file.
The java code is :
buildCommand.add("Rscript ");
buildCommand.add(scriptName);
buildCommand.add(inputFileWithPathExtension);
buildCommand.add(uniqueIdForR);
I have seen other post but I am unsure wether it will help in my case, also similar posts talking about rJava package`, but didn't get clear idea.
Any help will be highly appreciated. thanks in advance !
Here a very simple example for reading command line arguments in your case:
args <- commandArgs(TRUE)
input <- args[1]
output <- paste0(args[2], ".txt")
cat("Reading from", input, "\n")
cat("Writing to", output, "\n")
Example:
$ Rscript foo.R foo.csv 1234567
Reading from foo.csv
Writing to 1234567.txt
I have a java program which i am running on a Unix/Linux server. It generates a .txt file as the output which contain some long text strings which are being fetched from a DB table.
I need one statement to appear in a single line so that no record statement gets continued in the next line in the output file.
I tried the following change in my code but it didn't help:
String s = "This is a String\nand all newline cha||rs\nshould be replaced in this example.";
System.out.println(s);
System.out.println(s.replaceAll("(\\r|\\n|\\r\\n)+", " "));
although the above method works when i run the program on my widnows machine, it doesn't work on the server!!
I am using print stream to print my console output as a result. Below is my code:
printstream myconsole=new printstream(new fileoutpoutstream(filelocation));
system.setOut(myconsole)
myconsole.print("module"+","+"status"+","+"result");
Now this will create a new file with a single sheet and print module, status and result as a column header and all of my console will follow these; example:
search clicked pass will be following module status and result respectively.
I completed these for one of my module with 20 outputs. Now I wanna try it for another module. So I want to print my console output in same file but in different sheet. How should I do that? Could you please help me?
//Java Code to export as CSV the using FileWriter.
Java Code :
FileWriter fileWriterForCsv;
fileWriterForCsv.append(4.500);
fileWriterForCsv.append(",");
fileWriterForCsv.append("Mani");
fileWriterForCsv.append(",");
fileWriterForCsv.append("March");
// Content of the CSV File Exported mentioned below
Generated CSV
______________
Actual Result : 4.5,Mani,March
Expected Result : 4.500,Mani,March
Please let me know whether i need to change the java code?? or how to proceed to get the expected result as above mentioned
Also tried to change the column type as text in CSV template. Not getting the expected result.
Try formatting the number output. This should write the whole line with a CRLF at the end.
fileWriterForCsv.format("%.3f,%s,%s%n", 4.5, "Mani", "March");