• 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

Print String from an Array

 
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have an array with given string that has two fields. I want to check the data in the field one and print data in filed two. So my array has following elements:-
{
CINSTALLDIR CM
CINSTALLDIR CT
CINSTALLDIR PJ
CINSTALLDIR RM
CINSTALLDIR RX
CINSTALLDIR SV
CINSTALLDIR TM
INSTALLDIR CT
INSTALLDIR PJ
INSTALLDIR TM
MINSTALLDIR CM
MINSTALLDIR RX
MINSTALLDIR SV
}

And the output i desire is:-
CINSTALLDIR CM CT PJ RM RX SV TM
INSTALLDIR CT PJ TM
MINSTALLDIR CM RX SV

So what i did till now is this as below, I am not able to apply the proper logic. Could you please guide me what i am doing wrong? or some other approach.

Thanks
Aditya

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What you are doing here ?
Regex[0].contains(Regex[0] ?



comparison should be with test[i]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will the input array always be sorted, or at least grouped? if if not, i'd sort it so that it is.

Then, read the first element, and break it apart. build the temp string "CINSTALLDIR CM".

read the next element, and break it apart. if the oldFirstToken.equals(newFirstToken), append the newSecondToken to the string.
otherwise, print the temp string, clear it out, and start rebuilding it.

when you get to the end of the array, make sure you print out your last temp string.
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrote this at this moment. Will work more on it 2moro to delete all the elements from the string buffer. Expert Comments to improve are appreciated.

Thanks
Aditya

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not "2moro", thank you very much.
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about this:--

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
question:

are you writing all this code at once, and only then compiling/running/testing?

Do you have a plan?

the WORST way to write software is to write your whole program in one go. Plan out exactly how it should work. For example, if I wanted to read a file and print every line that has my name in it to the screen and save the other lines to a new file, i may start with this plan:



reading through this, I see an immediate problem...line 8 says 'write to a new file' but i don't know anything about any other file here. I'd go back and revise this pseudo-code. I'd probably spend more time coming up with this than anything else on my project.

then, and this is the MOST important part...i'd write as little code as I could before i compile and test the heck out of it. my first go is always nothing more than writing a main method that prints "i'm in main". once that works, I add a single piece. I'd do nothing more than the file open on the input file...and print a success or fail. compile and test.

then i'd try reading each line of the file, and printing it tot he screen. compile and test.
Then I'd try testing each line for 'fred' and print 'true' or 'false' for each line. compile and test.
then I'd try only printing the lines with 'fred' - compile and test.
etc.

 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I have created another solution to the program shall I post here?
I am asking because here we guide to learn java instead directly posting answers.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think you should post a brief solution.
 
Ninad Kulkarni
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution to program is given below



Read comments in code
 
reply
    Bookmark Topic Watch Topic
  • New Topic