This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Print String from an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Print String from an Array" Watch "Print String from an Array" New topic
Author

Print String from an Array

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

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

Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 118


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



comparison should be with test[i]


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Not "2moro", thank you very much.
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

how about this:--

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

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.

Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

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.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Yes, I think you should post a brief solution.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

The solution to program is given below



Read comments in code
 
GeeCON Prague 2014
 
subject: Print String from an Array