While Loops with Search Strings

I had a java lab in which I was to create a program that holds a list of names and telephone numbers, and allows the list to be searched. I have done this lab and turned it in. Now my professor gives a chance to earn extra credit on our class grade by fixing the "mistakes" that we make on our assignments. I put mistakes in parenthesis b/c while my program worked, he believed I should have done it differently.

Here is my code:

import javax.swing.JOptionPane;

import java.io.*;

publicclass Lab12{

publicstaticvoid main( String[] args )throws Exception{

String[][] info = arrayFile();

for (String searchString = getSearchString(); searchString !=null; searchString = getSearchString()){

if (searchString ==null){

System.exit(0);

}

int i;

for (i = 0; i < info.length && !searchString.equals(""); i++){

boolean findline = info[i][0].toLowerCase().startsWith( searchString.toLowerCase() );

if (findline){

System.out.println(info[i][0]+"\t" + info[i][1] +"\t" + info[i][2]);

break;

}

}

if (i == info.length){

System.out.println("There is no match in the list for: " + searchString);

}

}

}

publicstatic String[][] arrayFile()throws Exception{

String fileName = JOptionPane.showInputDialog( null,"Enter filename to read with file path:","phone.txt" );

BufferedReader readIn =new BufferedReader(new FileReader( fileName ));

String input = readIn.readLine();

int i;

for (i = 0; input !=null; i++){

input = readIn.readLine();

}

readIn.close();

readIn =new BufferedReader(new FileReader( fileName ));

String[][] array =new String[i][3];

for (i = 0; i < array.length; i++){

input = readIn.readLine();

String[] lineArray = input.split("\t");

array[i][0] = lineArray[0];

array[i][1] = lineArray[1];

array[i][2] = lineArray[2];

}

readIn.close();

return array;

}

publicstatic String getSearchString(){

String returnStr = JOptionPane.showInputDialog( null,"Enter the name to search:",null);

return returnStr;

}

}

This was the only correction my professor suggested:

The loop would probably be better as a while loop. Then you wouldn't need

that test in

line 15 for searchString == null, which is only useful if CANCEL is hit the

very first time

the input is requested.

The problem is that I for the life of me can't figure out how to implement a While Loop in this situation.

[4579 byte] By [newbie_dooby_dooa] at [2007-11-26 12:21:25]
# 1
I think your professor meant to say something like:while(returnStr != "exit"){ }MeTitus
Me_Titusa at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...
# 2
Never compare Strings with == !!! (or !=)
CaptainMorgan08a at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...
# 3
Why not? ;)MeTitus
Me_Titusa at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...
# 4

> Why not? ;)

Run this:

public class StringsTest

{

public static void main(String[] args)

{

String a = "abc";

String b = "abc";

String c = new String("abc");

System.out.println("a == b: "+(a==b));

System.out.println("a.equals(b): "+(a.equals(b)));

System.out.println("b == c: "+(b==c));

System.out.println("b.equals(c): "+(b.equals(c)));

}

}

== checks to see if both variables point to the same reference. The equals() method actually checks the content in the Strings.

CaptainMorgan08a at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...
# 5
http://www.javapractices.com/Topic18.cjpThanks CaptainMorgan08,MeTitus
Me_Titusa at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...
# 6

> > Why not? ;)

>

> Run this:

> [code]

> public class StringsTest

> {

>public static void main(String[] args)

> {

>String a = "abc";

> String b = "abc";

>String c = new String("abc");

>System.out.println("a == b: "+(a==b));

> System.out.println("a.equals(b):

> "+(a.equals(b)));

>

>System.out.println("b == c: "+(b==c));

> System.out.println("b.equals(c): "+(b.equals(c)));

>}

> de]

>

> == checks to see if both variables point to the same

> reference. The equals() method actually checks the

> content in the Strings.

Thanks once again,

MeTitus

Me_Titusa at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...
# 7

public class StringsTest

{

public static void main(String[] args)

{

String a = "abc";

String b = "abc";

String c = new String("abc");

System.out.println("a == b: "+(a==b));

System.out.println("a.equals(b): "+(a.equals(b)));

System.out.println("b == c: "+(b==c));

System.out.println("b.equals(c): "+(b.equals(c)));

}

}

We have a txt file on our class's public drive that has the names in phone numbers in it. The user is to open that file and then search it by typing in a few characters of the person's name. The loop is just so that the user can continue to search until they cancel out. Does this code do this?

newbie_dooby_dooa at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...
# 8
> Does this code do this?Not at all. It was just a quick demonstration for MeTitus.
CaptainMorgan08a at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...
# 9

Ok, didn't think so, just double checking.

Would this work theoretically perhaps:

int i=0;

while (i = 0; i < info.length && !searchString.equals(""); i++) {

boolean findline = info[i][0].toLowerCase().startsWith( searchString.toLowerCase() );

if (findline) {

System.out.println(info[i][0]+ "\t" + info[i][1] + "\t" + info[i][2]);

break;

}

if (i == info.length){

System.out.println( "There is no match in the list for: " + searchString);

}

}

newbie_dooby_dooa at 2007-7-7 15:13:01 > top of Java-index,Archived Forums,Socket Programming...