Solved

Java Page Break Every 3 Lines

Posted on 2009-07-15
15
329 Views
Last Modified: 2013-11-23
Right now I am doing some screen scraping of a site.  I want to be able to format the data properly in the txt file, but can't seem to get it to work.  The end result that I want is this:

text1, text2, text3
text4, text5, text6

So 3 items would be printed out, the 1st two with a comma after each, then the last one with no comma and go to the next line.

Right now my code below produces this:

text1, text2, text3, text4, text5, text6,

Any thoughts or suggestions would be appreciated!
// Output a message to the log so we know that we'll be writing the text out to a file.
session.log( "Writing data to a file." );
 
// Create a FileWriter object that we'll use to write out the text.
out = new FileWriter( "form_submitted_text.txt", true );
 
// Write out the text.
out.write( session.getVariable( "Identifier" ) );
out.write (", ");
 
// Close the file.
out.close();

Open in new window

0
Comment
Question by:bschwarting
  • 8
  • 3
  • 2
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24861274
Use a PrintWriter on your FileWriter and do out.println();
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24861285
very new to java, can you give an example please CEHJ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24861369
Hang on, what's the value of

>> session.getVariable( "Identifier" ) ;

?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 1

Author Comment

by:bschwarting
ID: 24861384
text1
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24861393
it loops through and then gets "text2", then "text3"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24861467
>>it loops through

What does?
0
 
LVL 5

Expert Comment

by:sno0401
ID: 24861627
How can it loop through without any sort of loop/control logic? Is there more to your code than this?
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24861792
The screen scraping software calls this small piece of code each time their is something found in interactive.
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24861815
Sorry, when found in identifier
0
 
LVL 5

Expert Comment

by:sno0401
ID: 24862180
Not having the screen scraping software it is difficult to give an absolute answer but here is something i've written up to give you an idea of a way to go about it.
By dividing by 3 and getting the remainder, you are able to decide which way to print the word, whether it is followd by a comma or followed by no comma but a new line instead.
Hope this helps.
public class Lister {
 
	public static void main(String[] args) {
		String[] words = {"text1", "text2", "text3", "text4", "text5", "text6"};
        int size = words.length;
 
        for (int x = 0; size > x; x++)
        {
            String currentWord = words[x];
 
            int y = x % 3;
            if (y == 0 || y == 1) {
                System.out.print(currentWord +", ");
            } else {
                System.out.println(currentWord);
            }
        }
    }
}

Open in new window

0
 
LVL 3

Accepted Solution

by:
SordSord earned 500 total points
ID: 24862205
You can add another variable to the session object to keep track of the times your function has been called.

Also, depending on how the rest of your code is structured, there may be much easier ways of doing the same thing.
session.log( "Writing data to a file." );
out = new FileWriter( "form_submitted_text.txt", true );
out.write( session.getVariable( "Identifier" ) );
 
// get or create WriteCount
Integer writecount = session.getVariable("WriteCount");
if (writecount == null) {
  writecount = new Integer(0);
}
 
// increment WriteCount
writecount = new Integer(writecount.intValue()+1);
session.putVariable("WriteCount", writecount);
 
// determine if line break or comma is needed
if (writecount.intValue()%3 == 0) {
  new PrintWriter(out).println();
} else {
  out.write (", ");
}
 
out.close();

Open in new window

0
 
LVL 1

Author Comment

by:bschwarting
ID: 24863669
sord, this looks exactly like what i need.

when i run it, i get this error:

"method putVariable not found"
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24863724
The error message was: Error in method invocation: Method putVariable( java.lang.String, java.lang.Integer ) not found in class'com.screenscraper.scraper.ScrapingSession' : at Line: 13.
0
 
LVL 3

Assisted Solution

by:SordSord
SordSord earned 500 total points
ID: 24864097
Try setVariable( String identifier, Object value ) instead.
0
 
LVL 1

Author Closing Comment

by:bschwarting
ID: 31603839
pure genius!  i love EE!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
throw exception 21 44
by zero exception 10 41
jqury 17 30
Create an automated page index 9 19
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question