Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Grouping of records uisng Java

 
Vasant Chandra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with EJB. Moving this to the "Java in General" forum.
Please CarefullyChooseOneForum before posting.
 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about using while (myScanner.hasNextLine())...?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic