read two line once
we know in a text file reading one line is easy.
how about read two lines, i forget it.
please give me a hint.
tkx.
try
{
BufferedReader br =new
BufferedReader(new FileReader(Filename));
String line;
while ((line=br.readLine())!=null)
{
String[] result1 = line.split("\\s");//line 1
// how to get line2
[689 byte] By [
ardmorea] at [2007-11-27 8:35:06]

br.readLine() is the method that actually does the reading, so just call that again after you get the first line. You'll have to check that the result isn't null after both calls, since the file could end after either read.
is thatString line2 =br.readline.readline
Something like this?
public static StringBuffer readLines(BufferedReader reader, int lines)
{
StringBuffer buffer=new StringBuffer();
String s;
try {
for (int i=0; i<lines;i++)
{
s=reader.readLine();
if (s!=null) {buffer.append(s).append(System.getProperty("line.separator");}
else {break;}
}
}
catch (IOException ioe)
{
//do something
}
return buffer;
}
Message was edited by:
xiarcel>
I'd make it a String[] instead of a StringBuffer so the caller wouldn't have to split the result.
> I'd make it a String[] instead of a StringBuffer so> the caller wouldn't have to split the result.Yeah.. I guess... Or a String[][] ?
> > I'd make it a String[] instead of a StringBuffer
> so
> > the caller wouldn't have to split the result.
>
> Yeah.. I guess...
>>
Or a String[][] ?
>>
(He's splitting each line, it looks like, into it's own String[])
> > > I'd make it a String[] instead of a StringBuffer
> > so
> > > the caller wouldn't have to split the result.
> >
> > Yeah.. I guess...
> >>
> [code]
> Or a String[][] ?
> /code]
> >>
>
> (He's splitting each line, it looks like, into it's
> own String[])
I'd probably also keep the reading and the splitting in separate methods for maximum reusability.
I'm not trying to **** all over your design, honestly. Just making helpful suggestions. :-)
If I am going to read a paragraph one time which has many lines but lines number is dynamic. How to do it?
such as
start
534.3950 319.0698
542.3000 305.3779
534.3950 291.6861
530.0895 291.6861
537.9945 305.3779
530.0895 319.0698
534.3950 319.0698
end
start
415.8200 510.7559
423.7250 510.7559
423.7250 510.7559
439.5350 483.3721
435.2295 483.3721
421.5722 507.0272
415.8200 507.0272
end
I want to read from start to end
BufferedReader br = // ...
List<List<String>> paragraphs = new ArrayList<List<String>>();
String line = null;
while ((line = br.readLine()) != null && line.equals("start")) {
List<String> paragraph = new ArrayList<String>();
while ((line = br.readLine()) != null && !line.equals("end")) {
paragraph.add(line);
}
paragraphs.add(paragraph);
}
// Now the paragraphs list contains lists of lines which make up paragraphs
dwga at 2007-7-12 20:31:32 >

How can I obtain two arrays from each pragraph?If every pragraph having the formatting as:start1 2 a1 3 b1 4 b1 5 b1 6 bendI want x[] = {1 1 1 1 1};y[] = {2 3 4 5 6};Thanks so much!
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
BufferedReader br = //...
String line;
while ((line = br.readLine()) != null && line.equals("start")) {
while ((line = br.readLine()) != null && !line.equals("end")) {
String[] parts = line.split(" ");
xList.add(Integer.parseInt(parts[0]);
yList.add(Integer.parseInt(parts[1]);
}
}
Integer[] x = xList.toArray(new Integer[xList.size()]);
Integer[] y = yList.toArray(new Integer[yList.size()]);
dwga at 2007-7-12 20:31:32 >

I really appreciate your help.
I have many pragraphs.
As I mentioned each paragraph has a similar format.
What I want is to use x & y array created by each paragraph to plot a shape.
How to implement this?
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
BufferedReader br = //...
String line;
while ((line = br.readLine()) != null && line.equals("start")) {
while ((line = br.readLine()) != null && !line.equals("end")) {
String[] parts = line.split(" ");
xList.add(Integer.parseInt(parts[0]);
yList.add(Integer.parseInt(parts[1]);
}
}
Integer[] x = xList.toArray(new Integer[xList.size()]);
Integer[] y = yList.toArray(new Integer[yList.size()]);
// I will use x and y to plot a shape.
// then obtain next x and y from the next "start" and "end" section
// to do the same thing.
How to modify your code?
BufferedReader br = //...
String line;
while ((line = br.readLine()) != null && line.equals("start")) {
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
while ((line = br.readLine()) != null && !line.equals("end")) {
String[] parts = line.split(" ");
xList.add(Integer.parseInt(parts[0]);
yList.add(Integer.parseInt(parts[1]);
}
Integer[] x = xList.toArray(new Integer[xList.size()]);
Integer[] y = yList.toArray(new Integer[yList.size()]);
// Do your plotting here using x and y arrays
}
dwga at 2007-7-12 20:31:32 >

yes, I will plot here.Every time x array and y array will be overwriteen.Thanks
Welcome
dwga at 2007-7-21 22:43:08 >

dwg, if I am going to plot somethings in another method.how to return x and y arrays?Thanks again!
dwg, I think you're doing his homework for him... but I'll contribute:
If you're going to use something inside two methods within the same class, I'd just make the "thing" you're using a field...
Then just make the one method assign values to it and the other can plot the points? I don't know. I'm real bad with Generics, which is what dwg is using.
> while ((line = br.readLine()) != null && line.equals("start"))
I think this will throw a NullPointerException, when readLine() returns null. Because unlike in C and C++, in Javathe statemant after the && is always executed, no matter whether the statement before the && is true.
can someone please reply on this
> can someone please reply on this
Well, you can see what happens yourself:
import java.io.*;
public class Foo {
static boolean testA(String line) {
System.out.print("testA ");
return line != null;
}
static boolean testB(String line) {
System.out.print("testB");
return !line.equals("silly test");
}
public static void main(String[] args) throws IOException {
BufferedReader br = new
BufferedReader(new FileReader("Foo.java"));
String line = "";
while (testA(line) && testB(line)) {
System.out.println();
line = br.readLine();
}
}
}
> > while ((line = br.readLine()) != null &&
> line.equals("start"))
> I think this will throw a NullPointerException, when
> readLine() returns null. Because unlike in C and C++,
> in Javathe statemant after the && is always executed,
> no matter whether the statement before the && is true.
No that's not true. In java if the lhs expression of the && boolean operator evaluates to false the rhs expression is not evaluated. The same is not true about &.
Just try the following:String foo = null;
if (foo != null && foo.isEmpty()) {
System.out.println("Shouldn't be here");
}
if (foo != null & foo.isEmpty()) {
System.out.println("Wow, NPE!");
}
dwga at 2007-7-21 22:43:08 >

can u clarify me what actually "List<List><String>> " mean is this u mean to say List[List[String]]?
> can u clarify me what actually "List<List><String>> "
> mean is this u mean to say List[List[String]]?
No. The < and > are meant: they are used since Generics were introduced in Java 1.5:
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
I need help.
public class ..
private int xy[];
public int setxy() {
...
while ((line = br.readLine()) != null && line.equals(s1)) {
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
while ((line = br.readLine()) != null && !line.equals(s2)) {
String[] parts = line.split(" ");
xList.add(Integer.parseInt(parts[0]));
yList.add(Integer.parseInt(parts[1]));
}
Integer[] x = xList.toArray(new Integer[xList.size()]);
Integer[] y = yList.toArray(new Integer[yList.size()]);
// Plotting here using x and y arrays
int m = x.length;
for (int i = 0; i<m-1;i++)
{
xy[i]= x[i];
xy[i+m] = y[i];
// to return these values to class LinesRectsCirclesJPanel
}
return xy;
but
incompatible types found:int [] required: int
>
it should befor (int i = 0; i<m-1;i++) { xy[i]= x[i].intValue();xy[i+m] = y[i].intValue(); } >