Assaultus Maximus
Assaultus Maximus

Reputation: 5

while + Scanner with System.in + hasNext

I am supposed to write a program that processes users input and translates it into Pig Latin and prints it out. My instruction for translating to pig latin was:

Pig Latin is English with the initial consonant sound moved to the end of each word, followed by “ay.” Words that begin with vowels simply have an “ay” appended. For example, the phrase The deepest shade of mushroom blue would have the following appearance in Pig Latin: e-Thay eepest-day ade-shay of-ay ushroom-may ue-blay

So I wrote this program:

import java.util.Scanner;

public class Nothing
{
    public static void main(String args[])
    {
        System.out.println("Enter sentence that has to be translated into Pig Latin ");
        Scanner console = new Scanner(System.in);
        String token = console.next();
        String translated = processToken(token) + " ";

    while(console.hasNext())
    {
        token = console.next();
        translated = translated + processToken(token) + " ";
    }

    System.out.println(translated);
}

public static String processToken(String token)
{
    String processed = "";
    if(!(fC(token) == 'a' || fC(token) == 'e' || fC(token) == 'i' || fC(token) == 'o' || fC(token) == 'u' || fC(token) == 'y'))
    {
        if(fC(token) != 't')
        {
            processed = token.substring(1) + "-" + token.charAt(0) + "ay";
        }

        else
        {
            processed = token.substring(2) + "-" + token.substring(0, 2) + "ay";
        }
    }

    else
    {
        processed = token + "-ay";
    }

    return processed;
}

    public static char fC(String token) //returns first character in lowercase
    {
        return Character.toLowerCase(token.charAt(0));
    }
}

and it doesn't work :( I enter the input and it doesn't do anything :/ I tested it with a simple string instead of System.in in the Scanner and it worked. Help me please!

Upvotes: 0

Views: 4476

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your code works but might work better if you tweak it a little so that it completely processes and prints each line as they're entered. I suggest:

  • Getting each line with a while scanner has next line, and then nextLine()
  • Splitting the line into tokens via split
  • Processing and printing each token , with a space
  • println at the end of the while.
  • Adding some ending condition so your while loop knows when to exit and the program will then know when to end.
  • Don't forget to use equals(...) or equalsIgnoreCase(...) for your exit test, not ==. Or you can use contains(...)

e.g.

  System.out.println("Enter sentence that has to be translated into Pig Latin ");
  Scanner console = new Scanner(System.in);

  while (console.hasNextLine()) {
     String line = console.nextLine(); // get whole line
     String[] tokens = line.split("\\s+");  // split it on whitespace
     for (String token : tokens) {
        // process and print out each token
        System.out.print(processToken(token) + " ");
     }
     System.out.println();  // print out new line

     // check if line contains "exit"
     if (line.toLowerCase().contains(EXIT)) {
        break;
     }
  }
  if (console != null) {
     console.close();
  }

Where EXIT is public static String EXIT = "exit";

Upvotes: 1

David Rabinowitz
David Rabinowitz

Reputation: 30448

I think is waiting for additional input. Have sent an EOF signal, as you have no other termination logic in your code. You can run it in debug (with break-point in the loop to verify,

Upvotes: 2

Related Questions