Hi guys I'm trying to program a pig latin translator and I'm stuck in trying to prompt a user to enter the first vowel in the phrase that they entered. I feel like I'm not doing it properly.
When a user enters "white color"
I want the next two prompt to ask for the first vowel in the words. Then translate it to pig latin.
Any advice on how to get started? Thank you!
Here's my code:
import java.util.Scanner;
public class main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a 2 word phrase: ");
String input = sc.nextLine();
System.out.println("Please enter the first vowel in the first word: ");
String input1 = sc.nextLine();
System.out.println("Please enter the first vowel in the second word: ");
String input2 = sc.nextLine();
}
7 ways of generating pig latin are mentioned on Wikipedia.
Link : https://en.wikipedia.org/wiki/Pig_Latin.
So you don't need to ask the user for the vowels, you are the one whose supposed to identify the vowel. Choose one of the six ways, write a prototype and then come back if you are stuck.
Ps: I will recommend that you go with last way mentioned in the wiki as it is easier to implement.
Related
I'm a first year IT student and we recently had an activity that asks the programmer to create a program that will accept a sentence or a phrase, then detect if there is a space in that string and add a newline in between words that have a space after it.
For clarification, here's what the prompt should look like:
Enter a sentence or phrase: I am a student
then the output would be:
I
am
a
student
I tried to actually finish the code, but I was hit by a roadblock and got stuck. Here's my attempt though:
import java.util.*;
public class NumTwo{
public static void main (String[] args){
Scanner in = new Scanner(System.in);
String phr;
System.out.print("Enter a phrase: ");
phr = in.nextLine();
if(phr.contains(" ")){
System.out.print(phr + "\n");
}
else{
}
}
}
any comments to what i might have done wrong will be very much appreciated
EDIT:
I tried using sir Christoph Dahlen's solution which was to use String.replace, and it worked! Thank you very much.
You are currently checking weither the input line contains any spaces at all, but you have to react to every space instead.
One way would be to split phr by spaces and concatenating it with newlines.
String result = String.join(System.lineSeparator(), phr.split(" "))
So I have this exercise:
"Create a program that reads characters from the keyboard until it receives a dot.
The program must count the number of spaces.
Indicate the total number of spaces at the end of the program."
My problem is, everytime I type a character, I have to press "Enter", for it to register. This way it acknoledges the dot, but not the spaces, and it's also not practical, the point is to type the whole sentence, and ackowlegde the dot, and proceed with the rest of the code.
If I type the whole sentence (ex.: "I ate food."), it does not acknowledge the dot, and lets me keep writing.
This link: Java Scanner: stop reading after the first entry, suggests reading character by character, which is a thing I don't want.
This link: Java Scanner stop reading input if receives bad character, this one did not help me either.
package m1_praticas;
import java.util.Scanner;
public class M1_Pratica3_ContarEspacos {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char input;
String sentence = "";
//int white_spaces = 0;
System.out.println("Escreva uma frase:");
do{
input = scanner.next().charAt(0);
sentence += input;
} while(input != '.');
//for(char c : sentence.toCharArray()){
//if(c == ' '){
//white_spaces++;
//}
//}
System.out.println(sentence);
System.out.println(white_spaces);
}
}
In short, what I want, is when the user is typing a sentence, there is someking of method that is reading the sentence charater, by character.
Once that method meets the dot, it performs the rest of the code. Is that possible, or do I always have to hit "enter", to register a key?
Thanks in advance.
I was looking around forums and found a helpful code on how to count lowercase letters in an inputted string. Thing is, after testing it, I saw it only counts lowercase letters within the first word typed. So, for example, if I type: HeRE the counter will say I've typed in 1 lowercase letter (which is correct), but if I type in: HeRE i am the counter will still only say 1 instead of 4. It's only counting the lowercase letters in the first word. How do I get it to count lowercase letters in my entire string?
Code thus far:
import java.util.Scanner;
public class countingLowerCaseStrings {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your string: ");
String input = scanner.next();
int LowerCaseLetterCounter = 0;
for (char ch : input.toCharArray()) {
if (Character.isLowerCase(ch)) {
LowerCaseLetterCounter++;
}
}
System.out.println ("Number of lower case letters in this string is: " +
LowerCaseLetterCounter);
}
}
Thanks a bunch for the help!
scanner.next(); reads the first available word, not the entire line.
So if you input "HeRE i am" it will just read "HeRE".
Change it to scanner.nextLine():
System.out.println("Enter your string: ");
String input = scanner.nextLine();
DEMO - look at stdin and stdout panels.
As a matter of interest, Java 8 provides a fairly streamlined way of achieving the same thing:
scanner.nextLine().chars().filter(Character::isLowerCase).count()
Alright, I'm coding this program that will discard any characters that are not letters. And right now I am having trouble trying to have the program identify which is which. Here's some of the code I did.
System.out.println("Press enter every time, you type a new word, and press the period button to end it.");
Scanner question = new Scanner(System.in);
System.out.println("Press enter to continue, or tupe something random in");
String userInput = question.next();
while(!userInput.equals(".")){
String userInput2 = question.next();
System.out.println(userInput2);
if(userInput2.equals("Stop")){
break;
}
}
You can use a regular expression to remove all characters which are not either lowercase or uppercase letters:
String userInput2 = question.next();
userInput2 = userInput2.replaceAll("[^a-zA-Z]", "");
System.out.println(userInput2);
Go through the string and call Character.isLetter(char) for each char to test if it is a letter character.
and while trying to make this simple program, im having trouble getting user input in terms of the string. When entering the integer, im having no problems, but when my program asks the user to enter another a character, the cursur will blink waiting for me to type in something, but it wont let me. If i comment out all of the integer stuff, i am then allowed to enter a string. Is there a reason i cant input both? thank you
import java.util.Scanner;
public class math {
public static void main(String args[]){
int int1,int2,int3;
String operator;
Scanner ahmad=new Scanner(System.in);
System.out.print("Enter three integers: ");
int1=ahmad.nextInt();
int2=ahmad.nextInt();
int3=ahmad.nextInt();
System.out.print("Enter a (for average), s (for sum) or p (for product):");
operator=ahmad.nextLine();
System.out.println("Thank you");
}
}
nextInt() only consumes the integer, it doesn't consume the whitespace characters (EOL in this case). Use two nextLine(), one to consume the EOL character, one to prompt you for input.
System.out.print("Enter a (for average), s (for sum) or p (for product):");
operator=ahmad.nextLine();
operator=ahmad.nextLine();
System.out.println("Thank you");