Shimy
Shimy

Reputation: 53

While loops aren't repeating

This program will read each phone number from the telephones.txt file and will check if it can be translated into one or more words of words.txt file. The output of the program will contain the telephone numbers and their word representatives. (assuming there are words and phones in the file)

    public static void main(String[] args) throws IOException
{
Scanner phones = new Scanner(new File("phones.txt"));
Scanner words = new Scanner(new File("words.txt"));      
PrintWriter outfile = new PrintWriter(new FileWriter("outfile.txt"));
String number = "", output = "", code = "" ;
//Scans for next phone string
while(phones.hasNext())
{
    number = phones.next();
    number = number.replace("-","");
    //Scans for next word string
    while(words.hasNext())
    {
        code = words.next();
        char[] wordChars = null;
        wordChars = code.toCharArray();
        output = "";
            //converts word to digits
        for(char wordChar : wordChars)
        {
            output = output.concat(new String(convert(wordChar)));
        }
        if(number.equals(output));
        {
            System.out.println(number + " " + code);
        }
        break;
    }
}
}

This is what I have done so far, except I can't figure out something

if(number.equals(output));
        {
            System.out.println(number + " " + code);
        }

For this line Im trying to see if the output has the same value as the number and if it does have the same value I want to print it out however this is what happens in my program.

I tried tracing my program and this is what i think is happening but its not...

Enter first while loop while theres a phone string continue

Set number to next phone string

Enter 2nd while loop while theres a word string continue

Set code to the CURRENT word string and convert it to a digit value (assume the conversion is correct)

If converted string isn't equal to number I want to continue the loop and search for the next word. And if it is I want to display number + code and continue searching for more words.

After i search through the list of words I want to continue to the next number and repeat the search for words for that number until the first while loop has no more numbers

Upvotes: 0

Views: 226

Answers (2)

amit
amit

Reputation: 178421

Answering your second issue:

After i search through the list of words I want to continue to the next number and repeat the search for words for that number until the first while loop has no more numbers

Answer:

You should create a new instance if scanner in each iteration, i.e. move the line:

Scanner words = new Scanner(new File("words.txt"));

into the body of the outer loop.

while(phones.hasNext())
{
    Scanner words = new Scanner(new File("words.txt"));  
    number = phones.next();
    number = number.replace("-","");
    //Scans for next word string
    while(words.hasNext())
    {
    ...
    }
}

Upvotes: 1

creichen
creichen

Reputation: 1768

Your inner loop always ends on a break. So the loop body will only ever get executed once. Just put your break into the block that describes the handling of a successful phone number match.

Upvotes: 1

Related Questions