Good day!
Kindly please someone tells me why my program generates same content of output file? They're supposed to have different contents.
Must be something wrong about my codes..help me to figure it out, Thank you very much... - for(int i=0; i < 5; i++) {
-
try {
-
output = new BufferedWriter( new FileWriter(FILENAME + ind + ".txt", true));
-
output.write(name+ " " + add+ " " + phone + "\n");
-
output.close();
-
}catch (IOException e)
-
{
-
System.out.println("There was a problem creating/writing to the temp file");
-
e.printStackTrace();
-
}
-
}
17 6326
Good day!
Kindly please someone tells me why my program generates same content of output file? They're supposed to have different contents.
Must be something wrong about my codes..help me to figure it out, Thank you very much... - for(int i=0; i < 5; i++) {
-
try {
-
output = new BufferedWriter( new FileWriter(FILENAME + ind + ".txt", true));
-
output.write(name+ " " + add+ " " + phone + "\n");
-
output.close();
-
}catch (IOException e)
-
{
-
System.out.println("There was a problem creating/writing to the temp file");
-
e.printStackTrace();
-
}
-
}
What have you done in your code to make your output different?
What have you done in your code to make your output different?
I put output.close(); outside try..
still the same...
I put output.close(); outside try..
still the same...
Shana, you are using a for loop to output to the file and the part of code you've posted has nothing to suggest that the output should be different.
Shana, you are using a for loop to output to the file and the part of code you've posted has nothing to suggest that the output should be different.
Ok r035198x..hope you can leave this message for a while.
I need to solve this first, please ... -
output.write(number + "\n");
-
output.write ("A:" +name);
prints output:
51 A
How to make it go to next line like this:
51
A
Ok r035198x..hope you can leave this message for a while.
I need to solve this first, please ... -
output.write(number + "\n");
-
output.write ("A:" +name);
prints output:
51 A
How to make it go to next line like this:
51
A
Wrap a PrintWriter around your BufferedWriter and use two separate println()s
for the fields.
kind regards,
Jos
Wrap a PrintWriter around your BufferedWriter and use two separate println()s
for the fields.
kind regards,
Jos
Got it Josah!
Thank you very much.
Allow me to ask one more question about array syntax please.. please
take a look at codes below: Thank you. - for(int ind=0; ind < 10; ind += 2)
-
{
-
b1 = arrayArgs[ind];
-
b2 = arrayArgs[ind+1];
-
-
switch (opcode)
-
{
-
case GCD: out = b1.gcd(b2).toString();
-
break;
-
default: throw new BigAlException("invalid operation");
-
}
-
String[] output = {out}; //this part - how to initiliaze out to an output array?
-
}
-
System.out.println(output); //so here I can print all of outputs from the array
-
Allow me to ask one more question about array syntax please.. please
take a look at codes below: Thank you. - for(int ind=0; ind < 10; ind += 2)
-
{
-
b1 = arrayArgs[ind];
-
b2 = arrayArgs[ind+1];
-
-
switch (opcode)
-
{
-
case GCD: out = b1.gcd(b2).toString();
-
break;
-
default: throw new BigAlException("invalid operation");
-
}
-
String[] output = {out}; //this part - how to initiliaze out to an output array?
-
}
-
System.out.println(output); //so here I can print all of outputs from the array
-
"out" is an int (I guess), so your array initialization should look like this: -
String output= { ""+out };
kind regards,
Jos
Allow me to ask one more question about array syntax please.. please
take a look at codes below: Thank you. - for(int ind=0; ind < 10; ind += 2)
-
{
-
b1 = arrayArgs[ind];
-
b2 = arrayArgs[ind+1];
-
-
switch (opcode)
-
{
-
case GCD: out = b1.gcd(b2).toString();
-
break;
-
default: throw new BigAlException("invalid operation");
-
}
-
String[] output = {out}; //this part - how to initiliaze out to an output array?
-
}
-
System.out.println(output); //so here I can print all of outputs from the array
-
"out" is an int (I guess), so your array initialization should look like this: -
String output= { ""+out };
The expression ""+out has type String so it can be an element of a String array.
kind regards,
Jos
I am stucked now on how to separate my txt file content. I need to print out them using BufferedWriter method....
I have these data in a txt file:
2 0 groupAStart
2 1 groupAStart //From here
3 2 james
5 3 jun
3 4 groupAfinish
8 5 groupAfinish //Until here - goes to output1.txt
3 6 junk
5 7 junk
2 8 groupAStart //from here
2 9 groupAStart
3 10 jello
5 11 juniady
2 12 groupAfinish
2 13 groupAfinish // Until here - goes to output2.txt
.....
.....
....
I don't know what is the syntax to make it work. As I have done this........please advise me..Thank you. - for(int i=0; i<=100 ; i++)
-
{
-
try
-
{
-
BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
-
-
for(int ind=0; ind<=159451;ind++)
-
{
-
String[] thelineFRST = lineFRST.split(" ");
-
String pos = thelineFRST[0];
-
String num = thelineFRST[1];
-
String group = thelineFRST[2];
-
-
if(method.equals("")) //I am clueless this part
-
{
-
bufferWriter = new BufferedWriter( new FileWriter("OutputFirst" +i + ".txt", true));
-
bufferWriter.write(pos+ " " +num+ " " + "\n");
-
bufferWriter.close();
-
}
-
}
-
printWriter.close();
-
inputFIRST.close();
-
....
-
Happy Labour Day!
"out" is an int (I guess), so your array initialization should look like this: -
String output= { ""+out };
The expression ""+out has type String so it can be an element of a String array.
kind regards,
Jos
Yes, out is an int (BigInteger), and don't you think that it should look like this -
String[] output= { ""+out };
But then, I still print nothing ....erm do you think it's good idea to initiliaze them in into array? Because my idea is to print all the output one short after finish calculating many input values.
Yes, out is an int (BigInteger), and don't you think that it should look like this -
String[] output= { ""+out };
But then, I still print nothing ....erm do you think it's good idea to initiliaze them in into array? Because my idea is to print all the output one short after finish calculating many input values.
My bad, I mistyped my answer (lame excuse: I haven't had my espresso yet).
You want a String array so it can be constructed as you showed above.
This gives you an array with one element in it which is the String representation
of variable 'out'. I apologize for the typo.
kind regards,
Jos
My bad, I mistyped my answer (lame excuse: I haven't had my espresso yet).
You want a String array so it can be constructed as you showed above.
This gives you an array with one element in it which is the String representation
of variable 'out'. I apologize for the typo.
kind regards,
Jos
hihiih..no worries ! I know that was a trick to test me ;)
I'll try again to make it works
Kindly please read my aboved message...I have problem on how to separate file contents.Thanks a bunch...
Kindly please read my aboved message...I have problem on how to separate file contents.Thanks a bunch...
Basically you have lines; each line contains three strings; when a line contains
a string "groupAStart" you want to start spooling the rest of the text to an
OutputStream or Writer until a line contains the String groupAEnd; right?
Something like this will get you started: -
public void spool(BufferedReader in) throws IOException { // read from this reader
-
int fileno= 1; // file number in "output#.txt"
-
String line;
-
PrintWriter out= null; // if null: don't print anything
-
while ((line= in.readLine()) != null) { // while next line available
-
if (line.indexOf("groupAStart") >= 0) {
-
if (out == null)
-
out= makeOutFile(fileno++); // build a PrintWriter
-
}
-
else if (out != null) {
-
out.println(line);
-
if (line.indexOf("groupAEnd") >= 0) {
-
out.close();
-
out= null;
-
}
-
}
-
// let caller close the in stream
-
}
Note that this little methods needs a couple of other methods, e.g. opening
a PrintWriter for the output given a serial number and it needs a 'driver' method
that opens the BufferedReader, calls this method and closes the reader again.
Instead of sticking all functionality in one big, ugly method better decompose
the problem like I did in this little example.
kind regards,
Jos
Phew..I've typed wrong in my program.
just finished deleted 27000 created output files in my dir. In the input file - there are 159451lines.
kindly please help me to figure it out ....really appreciate any help. thank you - public class Transfer
-
{
-
private static String lineFRST = "";
-
-
public static void main(String[] args) throws IOException
-
{
-
BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
-
spool(inputFIRST);
-
}
-
-
public static void spool(BufferedReader in) throws IOException
-
{
-
BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
-
-
PrintWriter printWriter = null;
-
BufferedWriter bufferWriter = null;
-
-
int fileno = 1;
-
lineFRST = inputFIRST.readLine();
-
-
while(lineFRST != null)
-
{
-
if (lineFRST.indexOf("groupAStart") >= 0)
-
{
-
if (printWriter == null)
-
makeOutFile (fileno++);
-
}
-
else if (printWriter != null)
-
{
-
printWriter.println(lineFRST);
-
if (lineFRST.indexOf("groupAFinish") >=0)
-
{
-
printWriter.close();
-
printWriter = null;
-
}
-
}
-
}
-
}
-
-
public static void makeOutFile(int fileno) throws IOException
-
{
-
PrintWriter printWriter = null;
-
BufferedWriter bufferWriter = null;
-
bufferWriter = new BufferedWriter( new FileWriter("output" +fileno +".txt", true)); //this not works...
-
-
String[] thelineFRST = lineFRST.split(" ");
-
String num1 = thelineFRST[0];
-
String num2 = thelineFRST[1];
-
String method = thelineFRST[2];
-
-
bufferWriter.write(num1+ " " + num2+ "\n");
-
bufferWriter.close();
-
}
-
}
Phew..I've typed wrong in my program.
just finished deleted 27000 created output files in my dir. In the input file - there are 159451lines.
kindly please help me to figure it out ....really appreciate any help. thank you - public class Transfer
-
{
-
private static String lineFRST = "";
-
-
public static void main(String[] args) throws IOException
-
{
-
BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
-
spool(inputFIRST);
-
}
-
-
public static void spool(BufferedReader in) throws IOException
-
{
-
BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
-
-
PrintWriter printWriter = null;
-
BufferedWriter bufferWriter = null;
-
-
int fileno = 1;
-
lineFRST = inputFIRST.readLine();
-
-
while(lineFRST != null)
-
{
-
if (lineFRST.indexOf("groupAStart") >= 0)
-
{
-
if (printWriter == null)
-
makeOutFile (fileno++);
-
}
-
else if (printWriter != null)
-
{
-
printWriter.println(lineFRST);
-
if (lineFRST.indexOf("groupAFinish") >=0)
-
{
-
printWriter.close();
-
printWriter = null;
-
}
-
}
-
}
-
}
-
-
public static void makeOutFile(int fileno) throws IOException
-
{
-
PrintWriter printWriter = null;
-
BufferedWriter bufferWriter = null;
-
bufferWriter = new BufferedWriter( new FileWriter("output" +fileno +".txt", true)); //this not works...
-
-
String[] thelineFRST = lineFRST.split(" ");
-
String num1 = thelineFRST[0];
-
String num2 = thelineFRST[1];
-
String method = thelineFRST[2];
-
-
bufferWriter.write(num1+ " " + num2+ "\n");
-
bufferWriter.close();
-
}
-
}
I don't understand what you're doing here:
1) you create a BufferedReader in you main() method, you pass it to the spool()
method and there you ignore it and open another BufferedReader. Why?
2) I my suggested code the makeOutFile method was just supposed to create
and return a PrintWriter; you decided to make it a void method. Why?
3) You write something in the makeOutFile method. Why?
4) Note that the printWriter is always null because you never assign something
to that variable? Why not?
kind regards,
Jos
I don't understand what you're doing here:
1) you create a BufferedReader in you main() method, you pass it to the spool()
method and there you ignore it and open another BufferedReader. Why?
2) I my suggested code the makeOutFile method was just supposed to create
and return a PrintWriter; you decided to make it a void method. Why?
3) You write something in the makeOutFile method. Why?
4) Note that the printWriter is always null because you never assign something
to that variable? Why not?
kind regards,
Jos
I managed to solve it Josah - by your advice of cause (after got so many Why Qs from you... :))
I am learning a lot from you.thank you very much
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Konrad Den Ende |
last post by:
I tried to write some Unicode-16 characters (that were displayed
correctly, as expected, on the screen) to a file but it didn't work
out very well. I have those in an char as well as a String. Both...
|
by: slukacs |
last post by:
hello one and all,
i am still new to java but becoming ever so more efficient. i need some
advice and guidance on big strings. i am debugging a special counter
that i created in java and i am...
|
by: Row |
last post by:
Hi,
I am having trouble writting from a JTextArea to a flat text file (locally)
I have tried using string tokenizer to tokenize the string first but with no luck,
Im stuck on the one :(
im...
|
by: slickn_sly |
last post by:
For some reason, I'm getting an error. It doesn't seem to be reading
my "input.txt" file. I'm trying to read the "input.txt" file which
consits of random integers and use insertion sort to sort...
|
by: asenthil |
last post by:
Hai to all,,
i had to tried to retrive and write a single row to a file from the database..
But dont know to write all the retrived rows to a file from the database..
how to do that...
...
|
by: PvtBillPilgrim |
last post by:
I need to write a method called save that takes a String as a parameter, and returns a boolean value. The parameter represents a filename, and the method should save the contents of one of my classes...
|
by: jebbyleezer |
last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty.
Here is my source code:
import java.io.*;
import java.util.Scanner;...
|
by: deeadeed |
last post by:
Hey,
Im working on a programme and need it to write to file. I put in a file writer it seems to run ok, creates the txt file but doesnt write any data. Heres the code:
private void...
|
by: minouparhizkar |
last post by:
hi could anyone helping me to finish this i have no idea how to implement the program in java im trying to grabbing the pixel from image and then convert it to the txt file .i did that but it didnt...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |