-2

So let's say I have a CSV file of random alphabets in columns for example's sake:

a
wrrq
jt
pkto
b   

They are in String format and I want to be able to find the minimum String length in this CSV list. In the example given, the minimum being 1 (the maximum being 4). Then I want to print all the minimums in order, resulting in:

a
b

This is my code that reads each line from the top and it's barely anything to a helpful guideline but I'm kind of stuck right now.

    BufferedReader br = new BufferedReader(new FileReader("Example.csv"));
    while ((nextLine = br.readLine()) != null) {

    ...

    }

Is there a way to do this a simple way without using fancy 3rd party scripts. Maybe putting them into an array and then working from there perhaps(?) Keeping in mind that the minimum may not always be 1.

Weissman
  • 11
  • 4
  • Yes, putting them in a List is a good idea. You can then iterate on that list as many times as needed. – JB Nizet Dec 06 '16 at 23:02
  • You can do it in a single iteration with a bit of trickiness, or pretty easily in two. Have you tried figuring out how to do it manually, without programming it? How did you get at that example output of a, b in your example? The first step is often to figure out how you reached your example answer, and then to generalize that, and as a _last_ step to program it. – yshavit Dec 06 '16 at 23:07
  • I have my pen and paper right now trying to pseudo it. I'm just drawing blanks. I'm thinking of conditions but they aren't working out so well- all scrap. – Weissman Dec 06 '16 at 23:17
  • 1
    The two-pass approach: (1) figure out the minimum length, and (2) print out all strings of that length. For the first one, try and figure out the algorithm yourself. If I gave you strings one at a time, how would you figure out their minimum length? Again, don't think of the Java yet -- just how would you, as a human, do it? – yshavit Dec 06 '16 at 23:25

1 Answers1

0

For a single-pass solution…

Create an empty collection to store results. Read first line, and store it in collection.

Read another line.

  • If this line is the same length as previously stored line, add to collection.
  • If longer, ignore.
  • If shorter, empty collection and then add.

Lather, rinse, repeat.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Personally I think that for homework assignments (as I'm guessing this is), giving the answer without the reasoning of how you came up with it is... not great. Programming is about how to break down problems much as it is about expressing algorithms in code. Just my two cents. – yshavit Dec 06 '16 at 23:41