While loop repeating unexpectedly [Java] - java

I am currently following a book on java programming and I tried to do one of the self study questions that asks to make a program that counts how many times you press the space-bar and you have to hit '.' to stop the while loop. However, the loop seems to want to loop over 3 times rather than asking to enter a key again after one. Here is the code
public class KeySelfTest {
public static void main(String args[]) throws java.io.IOException{
char input;
int space = 0;
input = 'q';
while (input != '.'){
System.out.println("Enter a key");
input = (char) System.in.read();
if (input == ' '){
space++;
}
else{
System.out.println("Enter a period to stop");
}
}
System.out.println("You used the spacebar key " + space + " times");
}
}
I was also wondering what I could use to initialize the input variable before the actual loop instead of just defaulting it to a random letter like q. Thanks for the help.

This is actually the perfect time to use a do-while loop.
do {
input = System.in.read();
...
} while (input != '.');

You can assign and test in one statement, like
char input;
int space = 0;
while ((input = (char) System.in.read()) != '.') {
System.out.println("Enter a key");
if (input == ' ') {
space++;
} else {
System.out.println("Enter a period to stop");
}
}
System.out.println("You used the spacebar key " + space + " times");

Related

How to fix this random number guessing game use a do-while loop program?

Create a program that randomly generates a number from 1-100 and asks the user to guess it. If the number the user inputs is to low or to high display a message to tell them so. When the user guesses the random number tell the user how much tries it took him to get that number. After that ask the user if they want to do it again if the user does repeat the process with a new random number generated.
The problem is that I can't seem to figure out how to let the user do it again, it seems to display an error in code when I run the program. If anyone can help me with this issue that would be great. Thank you!
import java.util.Scanner;
import java.util.Random;
public class RandomGuess
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
Random randy = new Random();
//#declaring variables
int num, count = 0;
final int random = randy.nextInt(100);
String input;
char yn;
//#random number
System.out.println("Num = " + random);
//#title or header
System.out.println("Random Number Guessing Game");
System.out.println("===========================");
//#asking user for input
do
{
System.out.print("Guess the random number " +
"from 1 to 100===> ");
num = keyboard.nextInt();
//#if the number the user entered
//#was less than the random number
if(num < random)
{
//#display this message
System.out.println("Your guess is too low try again...");
System.out.println();
}
//#if the number the user entered
//#was less than the random number
if(num > random)
{
//#display this message
System.out.println("Your guess is too high try again...");
System.out.println();
}
count++;
if (num == random)
{
System.out.println("You guessed the random number in " +
count + " guesses!");
break;
}
do
{
System.out.print("Continue? (Y or N)==> ");
input = keyboard.nextLine();
yn = input.charAt(0);
}
while(yn == 'Y' || yn == 'y');
}
while (num > 1 || num > 100);
}
}
There are a couple of problems with your code without even seeing the error that is displayed (I've put comments in those areas):
count++;
if (num == random)
{
System.out.println("You guessed the random number in " +
count + " guesses!");
break;
} // You should put an else here
do
{
System.out.print("Continue? (Y or N)==> ");
input = keyboard.nextLine();
yn = input.charAt(0);
}
while(yn == 'Y' || yn == 'y'); // This will keep asking if you want to try again so long as you enter a "y"
// But it won't actually let you try.
// Why? Because if you enter a y" it will loop back to the question.
}
while (num > 1 || num > 100); // This should probably be (random != num)
}
}
Here is a revised version
count++;
if (num == random) {
System.out.println("You guessed the random number in " +
count + " guesses!");
} else {
yn = 'x'; // can be anything other than y or n
while(yn != 'y' && yn != 'n') {
System.out.print("Continue? (Y or N)==> ");
input = keyboard.nextLine();
yn = input.toLowerCase().charAt(0);
}
}
}
while (num != random && yn == 'y');
}
}
Hopefully this is enough to move you forward.
Also, please post the error message and/or a description of what it is doing wrong along with a description as to what you actually wnt it to do.
As for the exception, the problem is that scanner.nextInt does not consume the newline at the end of the numbe you entered. So, your "continue Y/N" question gets what's left over from the previous line (i.e. a new line => an empty string).
You could try this:
num = -1; // Initialise the number to enable the loop
while (num <= 1 || num >= 100) {
System.out.print("Guess the random number from 1 to 100===> ");
String ans = keyboard.nextline();
try {
num = Integer.parseInt(); // Convert the string to an integer - if possible
} catch (NumberFormatException e) {
// If the user's input can not be converted to an integer, we will end up here and display an error message.
System.out.println ("Please enter an integer");
}
}

guessing letters in a randomized word and printing a progress result to the User. Cannot figure out how [Java]

I am currently trying to right a rough draft code block for an object oriented hangman assignment I have to tackle. I know that similar questions have been asked but I didn't find a solution for the specific way i'm attempting to write this (which quite possibly is actual shite).
The way the code is intended to work is as follows.
Word_to_guess pulls a word from the array wordBank[] which contains 19 different fruits and 1 pickle (this is just to entertain my professor).
Then, the following While loop checks if "noSuccess" has been falsified, while it remains true the loop asks for character input from the user as a guess.
then the nested for loop iterates through the length of word_to_guess to check if Characters at index i match character c which has been set to userGuess.charAt(0) (in case the user enters more than 1 letter restricting their guess to the first letter in the string they might enter).
now this is where I'm having my issues. I don't know how to use the .replace method to fill in spots with specific characters and store newWord for each iteration of the for loop and have it print a progress report to the user as they guess so that they can keep track of their guesses. I had attempted to use the following statement
newWord = newWord.replace(newWord.CharAt(i), c) but this did not work and caused a compiling error stating that the index could not be found. Which i didn't understand and still don't.
the final if statement checks if noSuccess has been falsified which is defined by booleancheck.contentEquals(word_to_guess)
booleancheck is defined by newWord without any spaces between characters.
If anyone can lend me a hand in sorting out that portion of the code or any other parts that look iffy I'd greatly appreciate it.
Thank you so much!
Alex
word_to_guess = wordBank[num];
boolean noSuccess = true;
while(noSuccess)
{
System.out.println("Enter your guess: ");
System.out.println("letters used - " + lettersUsed);
userGuess = keyboard.nextLine();
c = userGuess.charAt(0);
for(int i = 0; i < word_to_guess.length(); i++)
{
if(word_to_guess.charAt(i) == c)
{
if(newWord.charAt(i) == ' ')
{
newWord = newWord.replace(' ', c);
}
else if(newWord.charAt(i) == '_')
{
newWord = newWord.replace('_', c);
}
}
else
newWord = newWord.replace(' ', '_');
}
System.out.println(newWord);
counter ++;
lettersUsed = lettersUsed + c;
System.out.println("letters used - " + lettersUsed);
String booleanCheck = newWord.replaceAll("\\s+","");
if(booleanCheck.contentEquals(word_to_guess ) && counter > 1)
{
noSuccess = false;
System.out.println("Great job, you guessed the right letters!");
}
Update #1:
I came up with the following code:
for(int i = 0; i < word_to_guess.length(); i++)
{
if(word_to_guess.charAt(i) == c)
{
strProg.setCharAt(i, c);
}
else
{
strProg.setCharAt(i, '_');
}
newWord = strProg.toString();
}
but i'm not sure what I'm doing wrong with the .toString() method because each iteration replaces the previous progress with "_" instead of being additive.
Update: #2
Edit to the previous update, I read that using StringBuilder in this instance is better than StringBuffer. I also discovered that the append and insert methods are available to the StringBuilder object.
This string object is located outside of the while loop the for loop is in.
String newWord = String.join("", Collections.nCopies(word_to_guess.length(),
" "));
this is inside the while loop.
StringBuilder strProg = new StringBuilder(newWord);
for(int i = 0; i < word_to_guess.length(); i++)
{
if(word_to_guess.charAt(i) == c)
{
strProg.insert(i, c);
}
else if(word_to_guess.charAt(i) != c && newWord.charAt(i) == '_')
{
strProg.delete(i, i);
}
else
{
strProg.insert(i, '_');
}
}
the .insert method is resulting in a much longer string. I don't know what to do now.
//The bugs was on replace method: I used a naive way to fix it
//Maybe you need also to check your initialization ...
public void hangman(){
String word_to_guess = "pickle";//wordBank[num];
boolean noSuccess = true;
//start: initializations
char c;
String lettersUsed = "";
String userGuess;
Scanner keyboard = new Scanner(System.in);
int counter=0;
//end: initializations
String newWord = word_to_guess.replaceAll("(?s).", " ");
while(noSuccess)
{
System.out.println("Enter your guess: ");
System.out.println("letters used: " + lettersUsed);
userGuess = keyboard.nextLine();
//sanity check: To ignore empty entry
if(userGuess.isEmpty())
continue;
c = userGuess.charAt(0);
for(int i = 0; i < word_to_guess.length(); i++)
{
if(word_to_guess.charAt(i) == c)
{
if(newWord.charAt(i) == ' ')
{
newWord = newWord.substring(0,i) + c + newWord.substring(i+1); //newWord = newWord.replace(' ', c);
}
else if(newWord.charAt(i) == '_')
{
newWord = newWord.substring(0,i) + c + newWord.substring(i+1);//newWord.replace('_', c);
}
}
else
newWord = newWord.replace(' ', '_');
}
System.out.println("New Word: "+newWord);
counter ++;
lettersUsed = lettersUsed + c;
System.out.println("letters used: " + lettersUsed);
String booleanCheck = newWord.replaceAll("\\s+","");
System.out.println("Check: "+booleanCheck);
if(booleanCheck.contentEquals(word_to_guess ) && counter > 1)
{
noSuccess = false;
System.out.println("Great job, you guessed the right letters!");
}
}
}

How do I make a String unusable the second time in a loop?

I'm new to this site. I've decided to create a console base hangmaan game and I've been doing ok up till now. My current problem has me stuck.
I'm trying to make it so that if the user has input a letter and it has been marked as correct or incorrect. The program should then not let the user input that same letter again in later iterations of the while loop.
The comments should give you a better idea of what I'm talking about.
Can anyone please help me?
package hangMan2;
import java.io.IOException;
import java.util.Scanner;
public class mainClss {
public static void main(String[] args) {
int point = 0;
int numberOfLetterInWord;
// prompt user for input and store input into String word
System.out.println("Enter a word in lower case from the dictionary: ");
Scanner inputWord = new Scanner(System.in);
String word = inputWord.next();
// letters remaining in word equals the length of the word at the start
int lettersRemainingInWord = word.length();
for (int i = 0; i < 30; i++) {
System.out.println("\b");
}
// while points are above 7 (7 is when man is hung) and there's more
// than one letter remaining do all the following:
while (point > -7 && lettersRemainingInWord >= 1) {
//prompts user to input letter guess and stores in letter
System.out.print("\nEnter a letter for this " + word.length()
+ " letter word: ");
Scanner inputLetter = new Scanner(System.in);
String letter = inputLetter.next();
if (word.contains(letter)) {
System.out.println("Correct!");
point += 1;
System.out.println("Score: " + point);
lettersRemainingInWord -= 1;
//I need code here to remove the current letter from being able to be used again
if (lettersRemainingInWord > 0) {
continue;
}
else {
System.out.println("\nYou win!!!");
System.out.println("The word was: " + word);
break;
}
}
else {
System.out.println("Incorrect\n");
point -= 1;
System.out.println("Score: " + point);
//I need code here to remove the current letter from being able to be used again
if (lettersRemainingInWord > 0) {
continue;
}
else {
System.out.println("Game over! You lose!");
System.out.println("Score: " + point);
System.out.println("The word was: " + word);
break;
}
}
}
if (point <= -7) {
System.out.println("Game over! You lose!");
System.out.println("Score: " + point);
System.out.println("The word was: " + word);
}
}
}
You could test whether the letter is in a Set. If not, accept it and add it to the set. If so, then reject it.
Set<String> usedLetters = new HashSet<String>();
boolean isUsedLetter(String letter) {
if (usedLetters.contains(letter)) {
return true;
} else {
usedLetters.add(letter);
return false;
}
}
You can use an ArrayList to hold the characters that have already been typed and then check the list to see if the character is in there.
List<Character> used = new ArrayList<Character>();
char let = //letter that they inputted
if(used.contains(let)) {
//do something
}
else {
used.add(let);
}

When the user inputs to repeat the code, I get an error

I get this error when I try to repeat the code.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
Type a sentence and this program will tell you
how many vowels there are (excluding 'y'):
at java.lang.String.substring(String.java:1934)
at countvowels.Main.main(Main.java:53)
Java Result: 1
Here is my code:
Scanner input = new Scanner(System.in);
int answer = 0;
System.out.println("Count Vowels \n============");
// the do-while loop makes sure that the code is executed at least once
do {
if(answer == 1) {
System.out.println("You have chosen to count the vowels in another phrase");
}
System.out.println("Type a sentence and this program will tell you\nhow many vowels there are (excluding 'y'):");
String string1;
string1 = input.nextLine();
string1 = string1.toLowerCase();
int vowels=0;
int i = 0;
for (String Vowels : string1.split(" ")) {
for (i = 0; i < Vowels.length(); i++) {
int letter = Vowels.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i'
|| letter == 'o' || letter == 'u') {
vowels++;
}
}
System.out.println(Vowels.substring(0,1)
+ Vowels.substring(1) + " has " + vowels + " vowels");
vowels = 0;
}
System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
} while(input.nextInt() == 1);
System.out.println("Have a nice day");
}
}
I suspect you are getting this exception, when you have a word that contains just a single character. To eliminate this, and to also enhance your code, I would replace this line:
System.out.println(Vowels.substring(0,1) + Vowels.substring(1) + " has " + vowels + " vowels");
with:
System.out.println(Vowels + " has " + vowels + " vowels");
Also, modify the loop ending with the following:
nextInt(), reads just the next integer value.. So the line breaker stays in the buffer, and is later processed by the nextLine() command.
Modify your loop ending with the following:
answer = input.nextInt();
input.nextLine();
}
while (answer == 1);
I think your error come from your use of charAt()
This exception is thrown when the index is equal to the size of the string.

So I have this code here, it calculates the vowels in a given phrase. I'm trying to get it to repeat if the user says yes

I was wondering as to how I could get the end of the program to repeat if the user does respond with a 1. Do I need to reorganize it so that it is part of the if statement?
Scanner input = new Scanner(System.in);
System.out.println("Count Vowels \n============");
System.out.println("Type a sentence and this program will tell you\n\nhow many vowels there are (excluding 'y'):");
String string1;
string1 = input.nextLine();
string1 = string1.toLowerCase();
int vowels = 0;
int answer;
int i = 0;
for (String Vowels : string1.split(" ")) {
for (i = 0; i < Vowels.length(); i++) {
int letter = Vowels.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
vowels++;
}
}
System.out.println(Vowels.substring(0, 1).toUpperCase() + Vowels.substring(1) + " has " + vowels + " vowels");
vowels = 1;
}
System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
answer = input.nextInt();
if (answer == 1) {
System.out.println("You have chosen to count the vowels in another phrase");
} else {
System.out.println("Have a nice day");
}
You can do this with a do/while loop. The skeleton for this kind of loop looks like this:
Scanner input = new Scanner(System.in);
do {
// do your stuff here
System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
} while(input.nextInt() == 1);
System.out.println("Have a nice day");
It asks the user and evaluates the entered number in the while(input.nextInt() == 1) statement. If this comparison returns true (i.e. user entered 1), then the loops starts again. If not (i.e. user entered something else than 1), the loop stops and you'll get the "Good Bye" message instead.
you can split this up into more than one method and using one primary method call other methods inside a while loop. for example:
boolean continueCounting = false;
void countingVowels() {
//some start game method to make continueCounting = true
//something like "press 1 to start"
//if (input == 1) { continueCounting = true; }
while(continueCounting) {
String userInput = getUserInput();
countVowels(userInput); //count vowels in word from user input and prints them out to console
getPlayAgainDecision(); //ask user to put 1 or 2
if (answer == 1) {
continue
} else if (answer == 2) {
continueCounting = false;
} else {
System.out.println("incorrect input, please choose 1 or 2");
}
}
}
There are many ways to do this. A search on Google would have lead you to the correct answer in less time than it took you to ask the question. However, since you took the time to ask the question here is the answer:
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int answer = 0;
System.out.println("Count Vowels \n============");
// the do-while loop ensures that the code is executed at least once
do {
// on the first run answer equals zero, but on other runs it will equal one
if(answer == 1) {
System.out.println("You have chosen to count the vowels in another phrase");
}
System.out.println("Type a sentence and this program will tell you\n\nhow many vowels there are (excluding 'y'):");
String string1;
string1 = input.nextLine();
string1 = string1.toLowerCase();
int vowels = 0;
int i = 0;
for (String Vowels : string1.split(" ")) {
for (i = 0; i < Vowels.length(); i++) {
int letter = Vowels.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i'
|| letter == 'o' || letter == 'u') {
vowels++;
}
}
System.out.println(Vowels.substring(0, 1).toUpperCase()
+ Vowels.substring(1) + " has " + vowels + " vowels");
vowels = 1;
}
System.out.println("Would you like to check another phrase in the Vowel Counter? If so type 1 if not type 2 and press enter");
answer = input.nextInt();
} while (answer == 1);
System.out.println("Have a nice day");
}
}
In your code you assert that a letter is a vowel if it is in the set a, e, i, o and u which is true. However, the letter y can be a vowel in certain situations.
In general, the Y is a consonant when the syllable already has a vowel. Also, the Y is considered a consonant when it is used in place of the soft J sound, such as in the name Yolanda or Yoda.
In the names Bryan and Wyatt, the Y is a vowel, because it provides the only vowel sound for the first syllable of both names. For both of these names, the letter A is part of the second syllable, and therefore does not influence the nature of the Y.
You could expand on your code even more by checking if the letter y is a vowel or not.
This is a more elegant way to do the counting (I updated the code to satisfy Johnny's comment that my previous answer didn't answer OP's question. The code now loops without unnecessary code):
public static void main(String... args)
{
int answer = 0;
Scanner input = null;
do
{
input = new Scanner(System.in);
System.out.print("Type a sentence and this program will tell you\nhow many vowels there are (excluding 'y'):");
String sentence = input.nextLine();
int vowels = 0;
String temp = sentence.toUpperCase();
for (int i = 0; i < sentence.length(); i++)
{
switch((char)temp.charAt(i))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
}
}
System.out.println("The sentence: \"" + sentence + "\" has " + vowels + " vowels");
System.out.print("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press any other key... ");
String tempNum = input.next();
try
{
answer = Integer.parseInt(tempNum);
} catch (NumberFormatException e)
{
answer = 0;
}
System.out.println();
} while (answer == 1);
input.close();
System.out.println("Have a nice day");
}
Notice that at the end, I catch a NumberFormatException for more robustness validation of the user's input.
Just put the main for loop inside a do-while loop, like so:
do
{
for (String Vowels : string1.split(" ")) {
for (i = 0; i < Vowels.length(); i++) {
int letter = Vowels.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
vowels++;
}
}
System.out.println(Vowels.substring(0, 1).toUpperCase() +
Vowels.substring(1) + " has " + vowels + " vowels");
vowels = 1;
System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
answer = input.nextInt();
}
} while (answer == 1);
System.out.println("Have a nice day");
Additionally, there are better ways to do the counting, for example:
for (char c : string1.toCharArray())
{
c = Character.toLowerCase(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}

Categories

Resources