Take the 2-minute tour ×

I'm doing a relatively simple file read, and I've been experiencing anomalies in my validation between an input value and a data value read from a flat file and stored in an array. I've traced the error to the firstName field. I'm defining this field as equal to array[0]. While array[0] displays perfectly in my output, fieldName does not...it's either blank or null.

Here's my code, with an output sample.

I'm not sure if Stringbuilder is most appropriately used here? or one of the deep String methods? None of what I've tried seems to work. What am I missing...I know it's something obvious that I just can't see.

public class DisplaySelectedNumbers
    public static void main(String[] args)
        final String FN = "            ";
        final String LN = "            ";
        final String PHONE = "0000000000";
        String delimiter = ",";
        String s = FN + delimiter + LN + delimiter + PHONE + System.getProperty("line.separator");
        final int RECSIZE = s.length();
        String[] array = new String[3];
        Scanner kb = new Scanner(System.in);
        Path file = Paths.get("PhoneList.txt");
        String fName = JOptionPane.showInputDialog(null,"Enter first name to search");
            InputStream iStream=new BufferedInputStream(Files.newInputStream(file));
            BufferedReader reader=new BufferedReader(new InputStreamReader(iStream));
            while ((s = reader.readLine()) != null)
                array = s.split(delimiter);
                String dispString = array[0]+" "+array[1]+" "+array[2]+"\n";
                String firstName = array[0];
        catch(Exception e)
            System.out.println("Message: " + e);

share|improve this question
Lol, you seriously are not able to figure it out? :P This is what happens when you over think stuff :-) –  noMAD Apr 30 '12 at 17:56
indeed, indeed. It's been a long day. –  dwwilson66 Apr 30 '12 at 18:08
add comment

2 Answers

up vote 5 down vote accepted

You forgot to actually print the firstName variable:


You need instead:

System.out.println("firstName=" + firstName); 
share|improve this answer
Hey, guess what! It works! OMG. Can I downvote myself for being blind? Thansk for the second set of eyes. –  dwwilson66 Apr 30 '12 at 17:58
Please accept the answer if it helped solve your problem. –  Attila Apr 30 '12 at 18:01
Don't worry and no need to downvote. This can happen to anyone! –  Ido.Co Apr 30 '12 at 18:08
add comment

Not really a complete answer for your question, but yes, using StringBuilder in your code would be better if you need to improve String concatenation speed, specially in a loop.

StringBuilder vs String concatenation

share|improve this answer
add comment

Your Answer


