• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Grouping of records uisng Java

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 72
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has nothing to do with EJB. Moving this to the "Java in General" forum.
Please CarefullyChooseOneForum before posting.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about using while (myScanner.hasNextLine())...?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic