So im new to coding and i am having some issues... My program is supposed to ask the user for input, and will need to assume that all the input is lowercase... and need to assume there are no extra spaces, and will need to assume it ends with a period. The program will then translate the text into pig latin... Just incase you need the rules for pig latin they are if the word beging with a vowel, add a dash and "way" to the end... Otherwise, add a dash move the first letter to the end, and add "ay"... Now i know my code can be better but i just want to get it running first and then change it if i need too. So my issue is that the code runs but it will not print out any text besides the first word. And the other text has to also be in pig latin, i have pasted the code before. So any help would be awesome... Thanks.
import java.util.Scanner;
public class Piglat{
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
String text, pigLatin;
char first;
System.out.print("Enter a line of text: ");
text= scanner.next();
first = text.charAt(0);
if (first == 'a' || first == 'e' || first =='i'||
first == 'o' || first == 'u')
pigLatin = text + "-way";
else
pigLatin = text.substring(1) + "-" + text.charAt(0) + "ay";
System.out.println("Input : " + text);
System.out.print("Output: " + pigLatin);
}
}
My output
Enter a line of text: this is a text.
Input : this
Output: his-tay
text= scanner.next();
The scanner.next() method returns "the next token" which in this case likely means "the next word". Since your variable
text
only includes 1 word from the input sentence, that's what the rest of your code works with.You probably need to create a loop that reads from next() multiple times, or use something else than next() to read a complete line of text, which you then split into words and apply pig latin to each word.
The nextLine() method might be useful (but I can't say for sure, I haven't used this for a long time).
To split a line of text into individual words, look at How to split a string in Java and http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)