my dog learned polymorphism*
The moose likes Java in General and the fly likes Grouping of records uisng Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Grouping of records uisng Java" Watch "Grouping of records uisng Java" New topic
Author

Grouping of records uisng Java

Vasant Chandra
Greenhorn

Joined: Oct 06, 2012
Posts: 10
Hello All,
I have a requirement where i have to align the file in the below mentioned way using java. All the record will be in the same line untill the first character in next line starts with "E".

EABCDEFG
S11111111
S22222222
S33333333
EHIJKLMNO
S00000000
S99999999

The output file will be:

EABCDEFGS11111111S22222222S3333333
EHIJKLMNOS0000000S999999999


Thanks!!
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

well, you wont get a ready to use code here, try and write on your own using your logic and concepts , if you face any difficulty then post it.
Vasant Chandra
Greenhorn

Joined: Oct 06, 2012
Posts: 10
nir sharma wrote:well, you wont get a ready to use code here, try and write on your own using your logic and concepts , if you face any difficulty then post it.


I tried creating one code but it's not working for me. Please check

while(scanner.hasNext()){
String line = scanner.nextLine();

String out1="";
while (line.startsWith("E"))
{
while(line.startsWith("S"))
{
out1=out1+line.substring(0,15);
++processedLines;
line = scanner.nextLine();
}
writer.println(out1);
System.out.println(out1);
++processedLines;
line = scanner.nextLine();
}

Thanks!!
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

use code tag while posting your code, makes it easy to understand.

rather than using nested while cant you use if condition to check if character at 0 index or starting with 'E', and inside just append "\n" for new line to your String, and outside if block, keep appending the lines.
thats my suggestion to make your code simpler.
Vasant Chandra
Greenhorn

Joined: Oct 06, 2012
Posts: 10
nir sharma wrote:use code tag while posting your code, makes it easy to understand.

rather than using nested while cant you use if condition to check if character at 0 index or starting with 'E', and inside just append "\n" for new line to your String, and outside if block, keep appending the lines.
thats my suggestion to make your code simpler.


Thanks for your inputs.

Ok, i can use "if" block to append but how can i make the program to go again to the "if" block to check if the new line is starting with E or not?

Thanks!!
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

Vasant Chandra wrote:how can I make the program to go again to the "if" block to check if the new line is starting with E or not?

using the first while loop you have written for scanner.
Vasant Chandra
Greenhorn

Joined: Oct 06, 2012
Posts: 10
nir sharma wrote:
Vasant Chandra wrote:how can I make the program to go again to the "if" block to check if the new line is starting with E or not?

using the first while loop you have written for scanner.



Thanks for the much appreciated help. I have used below code but my program is going into infinite loop. can you please help me on this.

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

This has nothing to do with EJB. Moving this to the "Java in General" forum.
Please CarefullyChooseOneForum before posting.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2332
    
  50
Rather than concatenating a string inside a loop you should use a StringBuilder.

When faced with this type of problem add a few System.out.println("") statements to your code to print out the values of some of your variables so you can see what is happening.
BTW do you really want to read the next line twice for each iteration of the loop?
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

I have used below code but my program is going into infinite loop. can you please help me on this.

I wonder how thats happening, your program should rather throw an exception.
you are reading next line twice
you dont need to user substring method if you need complete line
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
What about using while (myScanner.hasNextLine())...?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
nir sharma wrote: . . . "\n" for new line to your String, . . .
Many books tell you to use \n and those books are wrong. It tells you here what to use instead.
As an alternative try If your output is full of occurrences of the word “null”, it means there are spelling errors in the code I posted
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Grouping of records uisng Java