aspose file tools*
The moose likes Java in General and the fly likes Urgent Need some basic help with converting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Urgent Need some basic help with converting" Watch "Urgent Need some basic help with converting" New topic
Author

Urgent Need some basic help with converting

Rassaad
Greenhorn

Joined: Aug 24, 2001
Posts: 3
A get an error that I can't convert a java.lang.String into a java.io.BufferedReader and that I can't convert a java.io.BufferedReader into a java.lang.String. Any solution in my modifying my code below to get it working

import java.io.*;
import java.util.*;
public class App4 {
public App4 ( ) throws IOException{
//URL anyURL = new URL("http://localhost/anyfile.ext");
//BufferedInputStream buf = new BufferedInputStream(anyURL.openStream());
//BufferedReader br = new BufferedReader(new FileReader(new File("final.txt")));
BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
BufferedWriter out = new BufferedWriter(new FileWriter(new File("finalTest.txt")));
StringTokenizer st;
String c="|";
//////////////////////////////////////////////////////////////////////////////////////////////
//Function that I'm thinking of creating to check stream for null values

int j =0;
while ((j=br.readLine().indexOf("| |")) > 0){
StringBuffer sb = new StringBuffer(br);
sb.replace(j,j+2,"|null|");
br = new String(sb);
/* App4.replace(a,aa); //Maybe by modifying this function it might work */
}
//Verify output
System.out.println(br);
/////////////////////////////////////////////////////////////////////////////////////////////
int temp;

while(true){
if( (c=br.readLine()) == null) break;
st = new StringTokenizer(c);
temp = st.countTokens();
for(int i = 0; i < (temp+1); i++){

if(i!=0){
//out.write(" " + st.nextToken());
System.out.print(" " + st.nextToken());
} else {
//out.newLine();
System.out.println(" ");
}
} //for
} //while

out.flush();
br.close();
out.close();
} //end of constructor
public static void main ( String args[] ) throws IOException {
new App4();
}//End of Main Method

// Another option would be to use this function
/////////////////////////////////////////////////////////////////////////////////
//This function should loop by replacing a value for another value
/* public String replace(char oldChar, char newChar)
{
char[] buff = new char[value.length];
boolean found = false;
for (int i = 0; i < value.length; i++)
{
char c = value[i];
if (c == oldChar)
{
found = true;
buff[i] = newChar;
}
else
buff[i] = c;
}
return found ? new String(buff) : this;
}
*/
////////////////////////////////////////////////////////////////////////////////////////////////
} // End of Application
------------------
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Rassaad
Your compilation errors are down to the variable you have named <code>br</code>. This is of type BufferedReader, hence when you attempt to pass it as an argument to the constructor of the StringBuffer class (<code>StringBuffer sb = new StringBuffer(br);</code>), the compiler complains, since there is no constructor that takes any type of Reader class.
You then appear to be taking the variable br and attempting to assign it to a new instance of String (<code>br = new String(sb);</code>), which is again illegal.
Try replacing that section with this:
<code>
//Function that I'm thinking of creating to check stream for null values
int j = 0;
String rawString = null;
String modifiedString= null;
while( ( rawString = br.readLine() ) != null )
{
if( (j = rawString.indexOf("| |")) > 0 )
{
StringBuffer sb = new StringBuffer(rawString);
sb.replace(j,j+2,"|null|");
modifiedString = sb.toString();
}
}
/* App4.replace(a,aa); //Maybe by modifying this function it might work */
//Verify output (:NB: This will only print out the last value)
System.out.println(modifiedString);
</code>
This should at least solve your compilation errors. I suspect that you may still have logical problems with this code, but since I don't really know what it is you're trying to do, I won't offer any more on that.
Hope this helps
Michael
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
"Rassaad",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
Sean
Rassaad
Greenhorn

Joined: Aug 24, 2001
Posts: 3
I have this data in a text file:
PM| ||1|XX|PID|HOT|PLAZA|NO. 4|Main Description|
and the code below gives me this data:
PM|null| |1|XX|PID|HOT|PLAZA|NO. 4|Main Description|
The final data should be:
PM|null|null|1|XX|PID|HOT|PLAZA|NO. 4|Main Description|
The code:
import java.io.*;
import java.util.*;
public class App4 {
public App4 ( ) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
BufferedWriter out = new BufferedWriter(new FileWriter(new File("finalTest.txt")));
StringTokenizer st;
String c="|";
//Function that I'm thinking of creating to check stream for null values
int j = 0;
String rawString = null;
String modifiedString = null;
while( (rawString = br.readLine() ) != null)
{
if( (j = rawString.indexOf("|")) > 0 )
{
StringBuffer sb = new StringBuffer(rawString);
sb.replace(j,j+2,"|null|");
modifiedString = sb.toString();
}
}
//Verify output (:NB: This will only print out the last value)
System.out.println(modifiedString);
out.flush();
br.close();
out.close();
} //end of constructor
public static void main ( String args[] ) throws IOException {
new App4();
}//End of Main Method

} // End of Application
------------------
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You really need to read Sean's message and register a new account.
As for your code - the original idea of looking for "| |" seemed much better. Your current code will simply take the very first "|" it finds, and replace it with "|null|", which doesn't sound like a good idea at all. For example the input
PM|somedata| |1|XX|PID|HOT|PLAZA|NO. 4|Main Description|
will be converted to
PM|null|medata| |1|XX|PID|HOT|PLAZA|NO. 4|Main Description|
Go back to searching for "| |" instead. If you want to find more than one "| |" in a line, you will of course need to use a loop to search for "| |" repeatedly.

"I'm not back." - Bill Harding, Twister
Robert, Assaad
Greenhorn

Joined: Aug 27, 2001
Posts: 12
My data.txt contains exactly this data below:
PM| | |1|XX|PID|HOT| | |PLAZA|NO. 4|Main Description|
it needs to be interpreted to:
PM|null|null|1|XX|PID|HOT|null|null|PLAZA|NO. 4|Main Description|
import java.io.*;
import java.util.*;
public class App7 {
public App7 ( ) throws IOException{

BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
int j = 0;
String rawString = null;
String modifiedString = null;
while( (rawString = br.readLine() ) != null)
{
//Commented out my if to a for next
//if( (j = rawString.indexOf("|")) > 0 )
//Changes made here, It does not work, Could you help
for(int i=0; rawString.indexOf("| |") > 0 ; i++ )
{
StringBuffer sb = new StringBuffer(rawString);
sb.replace(j,j+2,"|null|");
modifiedString = sb.toString();
}
}
//Verify output (:NB: This will only print out the last value)
System.out.println(modifiedString);
} //end of constructor
public static void main ( String args[] ) throws IOException {
new App7();
}//End of Main Method

} // End of Application

------------------
 
Consider Paul's rocket mass heater.
 
subject: Urgent Need some basic help with converting