java: hangman game repeating letters - java

I´ve got a logic problem with a hangman project, it takes an letter from the user and searches if that letter is contained within the secret word. The problem is the way I´ve programmed it, if there are several occurrences of the letter the user guessed within the secret word. it will just go through and denote them all. Which is not what I want, I only want it to update the status of correctly guessed letter one at a time.
I tried some different stuff like setting a break after status(guessCh, but then the iterator will just go to the first occurrence where the letters match and stop there.
any simple fix to this?
private void compare(String str)
{
guessCh = str.charAt(0);
char secretCh = '0';
for (int i = i2; i < secretWord.length(); i++) // Cuts the secret word into individual chars to process.
{
secretCh = secretWord.charAt(i);
// Compare the two strings.
if (guessCh == secretCh)
{
status(guessCh, i); // Sends the letter & placement to status().
}
}
}
n
private String status(char guessCh, int placement)
{
/* Update and return status. */
if (guessCh >='A' && guessCh <= 'Z')
{
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement,guessCh).toString();
println("That guess is correct.");
canvas.displayWord(status);
return status;
}
return status;
}

You could test for a prior solution using your status variable from inside your compare method.
if (guessCh == secretCh && status.charAt(i) != secretCh)
{
status(guessCh, i);
break;
}

From what I can read (and understand), the basic problem you're facing is caused by the for loop in the compare method.
(Note, my examples are case sensitive, you will need to take that into account)
There are two basic approaches I can suggest...
The first is, match ALL occurrences with a single check...
private char guessCh;
private String secretWord;
private String status;
private String secretBuffer;
public TestStringCompare() {
secretWord = "This is a test";
// This is a copy of the secret word, this ensures that
// we always have a copy of the original.
secretBuffer = secretWord;
status = "______________";
guessCh = 'i';
compare("i");
}
private void compare(String str) {
while (secretBuffer.contains(str)) {
int foundAt = secretBuffer.indexOf(str);
status(str.charAt(0), foundAt);
// We want to remove the "guess" from our check string
// so it doesn't cause a false positive in the future
StringBuilder sb = new StringBuilder(secretBuffer);
sb.replace(foundAt, foundAt + 1, "_");
secretBuffer = sb.toString();
System.out.println(secretBuffer);
}
}
private String status(char guessCh, int placement) {
/* Update and return status. */
if (Character.isLetter(guessCh)) {
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement, guessCh).toString();
System.out.println("That guess is correct.");
System.out.println(status);
}
return status;
}
Which would produce:
That guess is correct.
__i___________
Th_s is a test
That guess is correct.
__i__i________
Th_s _s a test
or, replace the first occurrence of the guess (which from what I understand is what you're after)
public class TestStringCompare {
public static void main(String[] args) {
new TestStringCompare();
}
private char guessCh;
private String secretWord;
private String status;
private String secretBuffer;
public TestStringCompare() {
secretWord = "This is a test";
secretBuffer = secretWord;
status = "______________";
guessCh = 'i';
compare("i");
}
private void compare(String str) {
if (secretBuffer.contains(str)) {
int foundAt = secretBuffer.indexOf(str);
status(str.charAt(0), foundAt);
// Some where here you need to remove the "guess" character
// to ensure that it doesn't get repeated...
StringBuilder sb = new StringBuilder(secretBuffer);
sb.replace(foundAt, foundAt + 1, "_");
secretBuffer = sb.toString();
System.out.println(secretBuffer);
}
}
private String status(char guessCh, int placement) {
/* Update and return status. */
if (Character.isLetter(guessCh)) {
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement, guessCh).toString();
System.out.println("That guess is correct.");
System.out.println(status);
}
return status;
}
}
Which would produce this...
That guess is correct.
__i___________
Th_s is a test

I am guessing this is homework of some sort - but anyway, why not use indexOf

Related

(Java) How do I make a database delete a stored value until the value has been stored more than 5 times?

I'm kinda trying to make a VERY basic replication of memory in a way.
Basically, I want my program to take in user input and forget it (delete it from database)after about 18 seconds, but remember it (permanently store it in database) if input is repeated 5 times or more.
Here's the code I have so far (It's a JavaFX program by the way):
TextField userText = new TextField();
Timeline time;
String message;
message = userText.getText();
ArrayList<String> memory = new ArrayList<>();
if(message.contains(message) && message.contains(" ") && !memory.contains(message)){
String[] splitMessage = message.split(" ");/*To split the sentence*/
for(int i = 0; i<splitMessage.length; i++)
memory.add(splitMessage[i]); /*To add the individual words of a sentence*/
memory.add(message); /*To add the sentence itself*/
time = new Timeline(new KeyFrame(Duration.millis(18000),
ae -> memory.remove(message)));
time.play();
}
So I have this so far and it works at storing data for 18 seconds. But I want it so the data is permanently stored into "memory" after the program has attempted to store the same message more than 5 times, whether consecutively or in random order.
Is there a way to do this?
Hope this made sense. I'm known for not wording questions properly haha.
Thanks in advance :).
This is my attempt at providing an additional layer of complexity to suffice your needs (if I understand them correctly).
I would replace:
ArrayList<String> memory = new ArrayList<>();
with
MemorySpace memorySpace = new MemorySpace();
The only problem that I see is the proper interpretation of:
!memory.contains(message);
It could be
memorySpace.isMemoryPermanent(message);
or
memorySpace.isMemoryActive(message);
Hopefully the API is clear enough for you to understand my intentions and how it could help in your situation. As I understand it a word is remembered permanently the first time but a sentence needs five time to become permanent.
public class MemorySpace {
private final Map<String, Memory> memorySpace;
public MemorySpace() {
this.memorySpace = new HashMap<>();
}
public void addWord(String word) {
Memory m = this.memorySpace.get(word);
if (m == null) {
this.memorySpace.put(word, new Memory(true, word))
}
}
public void addSentence(String sentence) {
Memory m = this.memorySpace.get(sentence);
if (m == null) {
this.memorySpace.put(sentence, new Memory(false, sentence))
}
else {
m.increaseSeenCount();
}
}
public boolean isMemoryPermanent(String workOrSentence) {
Memory m = this.memorySpace.get(wordOrSentence)
if (m != null) {
return m.isMemoryPermanent();
}
return false;
}
public boolean isMemoryActive(String workOrSentence) {
Memory m = this.memorySpace.get(wordOrSentence)
if (m != null) {
return m.isMemoryActive();
}
return false;
}
private class Memory {
private final boolean isWordMemory;
private final String wordOrSentence;
private int seenCount;
private long lastSeenAtMilliseconds;
Memory(boolean isWordMemory, String newWordOrSentence) {
this.isWordMemory = isWordMemory;
this.wordOrSentence = newWordOrSentence;
this.seenCount = 1;
this.lastSeenAtMilliseconds = System.currentTimeMillis();
}
boolean isWordMemory() {
return this.isWordMemory;
}
void increaseSeenCount() {
if (!this.isWordMemory) {
if (this.seenCount < 5) { // Stop overflow
this.seenCount++;
}
this.lastSeenAtMilliseconds = System.milliseconds();
}
}
void isMemoryPermanent() {
return this.isWordMemory || this.seenCount >= 5;
}
void isMemoryActive() {
return this.isWordMemory || this.isMemoryPermanent() || (System.currentTimeMillis() - this.lastSeenAtMilliseconds) < (18 * 1000);
}
}
}

Cannot Run *.java downloaded file

I have downloaded a java file needed for a coursework at college. However I find it impossible to run it. Eclipse won't give me the chance to even run it (only ant build), and if I use netbeans I get this exception :
Exception in thread "main"
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - class Hangman is public, should be declared in a file named Hangman.java
at Hangman. < clinit > (hangman(Case Conflict).java: 20)
Java Result: 1
If someone is kind enough to read through the code, I really do not know what to do next. I figure there has to be something wrong with the main class. Thanks!
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Hangman {
Scanner userInput;
private Set < Character > wrongGuesses;
private String[] answers = {
"leverets", "hatchlings", "puppies",
"kittens", "pullets", "goslings"
};
private String answer;
private String guessed;
private int maxTurns;
private int currentTurns;
private boolean inProgress;
private char nextGuess;
private boolean gameWin;
public Hangman() {
userInput = new Scanner(System.in);
wrongGuesses = new HashSet < Character > ();
inProgress = false;
gameWin = false;
maxTurns = 14;
currentTurns = 0;
// set answer somehow
answer = answers[0];
// set guessed to the correct number of dashes
StringBuilder sb = new StringBuilder();
for (int i = 0; i < answer.length(); i++) {
sb.append('-');
}
guessed = sb.toString();
}
/* start a new game */
public void startGame() {
inProgress = true;
startGameLoop();
}
/* the game loop. this method is the heart of the game */
private void startGameLoop() {
printInstructions();
while (inProgress) {
printStatus();
acceptGuess();
checkStatus();
}
printWinOrLose();
}
private void printInstructions() {
System.out
.println("Guess the word one letter at a time until you win or run out of turns. Good luck!");
}
private void printWinOrLose() {
if (gameWin) {
System.out.println("You win! The answer was " + answer);
} else {
System.out.println("You lose.");
}
}
private void printStatus() {
System.out.println("Guesses left: " + (maxTurns - currentTurns));
System.out.println("Current status: " + guessed);
System.out.println("Wrong guesses: " + getWrongAnswers());
}
/* get the next character from the player */
private void acceptGuess() {
System.out.println("Next guess: ");
String temp = userInput.next();
nextGuess = temp.charAt(0);
}
/* check what state the game is in */
private void checkStatus() {
// if already guessed, say already guessed.
if (wrongGuesses.contains(nextGuess)) {
System.out.println("You already guessed that!");
return;
}
// if guess is not in answer, update number of turns played and add
// guess to wrong guesses
// otherwise update the guessed variable
if (answer.indexOf(nextGuess) < 0) {
++currentTurns;
wrongGuesses.add(nextGuess);
} else {
updateGuessStatus();
}
// check to see if the player has won or lost
if (answer.equals(guessed)) {
gameWin = true;
inProgress = false;
}
if (currentTurns == maxTurns) {
inProgress = false;
}
}
/* update the guessed variable when there is a correct guess made */
private void updateGuessStatus() {
// replace - with nextGuess where appropriate
int index = answer.indexOf(nextGuess);
int lastIndex = answer.lastIndexOf(nextGuess);
StringBuilder sb = new StringBuilder(guessed);
if (index != lastIndex) { // more than one instance of the guess in the
// answer
// swap out in a loop
while (index != -1) {
sb.setCharAt(index, nextGuess);
int i = answer.indexOf(nextGuess, (index + 1));
index = i;
}
} else { // letter only appears once
// swap out just that one
sb.setCharAt(index, nextGuess);
}
guessed = sb.toString();
}
/* build a text representation of all the incorrect guesses */
private String getWrongAnswers() {
if (wrongGuesses.size() > 0) {
StringBuilder sb = new StringBuilder();
sb.append('(');
for (Character c: wrongGuesses) {
sb.append(c + ",");
}
sb.deleteCharAt(sb.length() - 1); // delete trailing comma
sb.append(')');
return sb.toString();
} else {
return "<none>";
}
}
public static void main(String[] args) {
Hangman h = new Hangman();
h.startGame();
}
}
The exception says everything you need to know. Rename the class FILE to Hangman.java.
Caused by: java.lang.RuntimeException: Uncompilable source code - class Hangman is public, should be declared in a file named Hangman.java
You should save your downloaded file in Hangman.java and not hangman.java (see it needs 'H' in caps same as your class name).
Change the class to public class Hangman. It allows outside methods to access it.
EDIT: I downloaded the file, changing the class to public worked. I also found an issue in the code itself, the word is always "Leverets".
To change this, edit the getAnswer() method and change it to
private int getAnswer() {
int i = (int) (Math.random() * 6) + 0;
return i;
}

Conditionally concatenate strings in for-loop, or use predefined string

I have the following HashMap:
private HashMap<String, Team> allTeams = new HashMap<String, Team>();
I want to return as a String all teams in my league, otherwise return a message "no teams in league".
I have written this code:
public String getTeam()
{
String x = "";
for(Team tm : allTeams.values())
{
if(tm.getStatus().equals("Added"))
{
x = x + tm.toString();
}
else
{
x = "there are no teams in your league";
}
}
return temp;
}
If I remove the "else" part of the conditional statement the code works.
However if I keep the "else" part, I continuously receive "there are no teams in your league" and I understand it is because once all teams have been returned there will be no further teams to return hence the "else" part of the statement is always printed.
How can I get this to work?
One way would be to check if x is empty outside of the loop, and set it if it is.
public String getTeam()
{
String x = "";
for(Team tm : allTeams.values())
{
if(tm.getStatus().equals("Added"))
{
x = x + tm.toString();
}
}
if (x.isEmpty())
{
x = "there are no teams in your league";
}
return x;
}
I assumed you meant to return x, not temp, as temp isn't defined here.

Constantly getting NullPointerException

I am making a program in which it encrypts words into some kind of secret message, but I keep on getting an error. Yes, I know the code is ugly... but I am a beginner.
Here is my main.
public class Main {
public static void main (String args []){
String message;
message = JOptionPane.showInputDialog("Give me something to crypt!");
Crypt secret = new Crypt();
secret.CyptedMessage(message);
}
}
Here is my class.
public class Crypt {
String letter[];
String message;
public Crypt(){
message = "";
letter[0]="A";
letter[1]="B";
letter[2]="C";
letter[3]="D";
letter[4]="E";
letter[5]="F";
letter[6]="G";
letter[7]="H";
letter[8]="I";
letter[9]="J";
letter[10]="K";
letter[11]="L";
letter[12]="M";
letter[13]="N";
letter[14]="O";
letter[15]="P";
letter[16]="Q";
letter[17]="R";
letter[18]="S";
letter[19]="T";
letter[20]="U";
letter[21]="V";
letter[22]="W";
letter[23]="X";
letter[24]="Y";
letter[25]="Z";
letter[26]=" ";
}
int getRandomCrypt(){
//Random number 1 to 25
int x;
x=(int)(1 + Math.random()*25);
return x;
}
int checkLetter(String subMessage){
//Checks letters
boolean b = false;
int i=0;
while (b = false){
if (i == 27){
i=0;
if (subMessage == letter[i])
return i;
else
i++;
}
}
return 0;
}
void CyptedMessage(String message){
String CyptedMessage = null;
String message1;
for (int i = 0; i < message.length(); i++){
int number = checkLetter(message.substring(i, i+1));
message1 = letter[number + getRandomCrypt()];
if (number + getRandomCrypt()>26){
message1 = letter[i-27];
}
CyptedMessage += message1;
}
System.out.print(CyptedMessage);
}
}
When I run this I get...
Exception in thread "main" java.lang.NullPointerException
at Crypt.<init>(Crypt.java:9)
at Main.main(Main.java:9)
As Serge pointed out, String letter[] is an array declaration, not an initialization. You need to then initialize it to define an exact size.
public Crypt(){
letter = new String[27]; //define array
message = "";
letter[0]="A";
letter[1]="B";
letter[2]="C";
letter[3]="D";
letter[4]="E";
letter[5]="F";
letter[6]="G";
letter[7]="H";
letter[8]="I";
letter[9]="J";
letter[10]="K";
letter[11]="L";
letter[12]="M";
letter[13]="N";
letter[14]="O";
letter[15]="P";
letter[16]="Q";
letter[17]="R";
letter[18]="S";
letter[19]="T";
letter[20]="U";
letter[21]="V";
letter[22]="W";
letter[23]="X";
letter[24]="Y";
letter[25]="Z";
letter[26]=" ";
}
Edit: forgot ;
And others beat me :P
You are not initializing your String letter[]; and directly adding values into it
do this in your constructor
letter = new String[size];
or do this in starting String letter[] = new String[size];
and in your case size is 27
You need to initialize you array ans not just define it.
String letter[] = new String[27];
Also you can put a NPE check before using the array data
if(letter[number + getRandomCrypt()] != null)
message1 = letter[number + getRandomCrypt()];
Your String letter[]; is not instantiated.
add letter = new String[size]; in your Constructor of Crypt before initialization.
In class Crypt, you declare a string array named letters, but you do not initialize it when you use it. That is why you encounter a NullPointerException.
Initilize it before you use it.
public Crypt(){
**letter= new String[27];**
letter[0]="A";
letter[1]="B";
... ...
}

Calling Two Different Methods into One Method in Java

I wish to:
Reading in two files
Split the files into individual strings
Compare the two string lists and retrieve strings that are unique to a file.
At the moment I am running in to the problem of finding a way to call the two methods used to call in the files (one for each file) to the same method in order to be compared.
Both methods use a try-catch-while statement and if I try to read all of the entries after the while statement only a single is shown and not the entire list.
Is there a way to send parts of both methods as parameter to a single new method?
Here is the code for the program. I know that there are problems with the way that I am doing the program, but I am only doing it the way that I was taught.
File mainEmails = new File("Testrun.txt");
Scanner inputScanner = null;
int counter = 1;
String fullName = null;
String position = null;
String companyName = null;
String telNumber = null;
String emailAddress = null;
try
{
inputScanner = new Scanner(mainEmails);
}
catch(FileNotFoundException e)
{
System.out.println("File has not been found.");
}
while (inputScanner.hasNextLine())
{
String nextLine = inputScanner.nextLine();
String [] splitFile = nextLine.split(",");
for (int i = 0; i <splitFile.length;i++)
{
if(i==0)
{
fullName = splitFile[0];
}
else if(i==1)
{
position = splitFile[1];
}
else if(i==2)
{
companyName = splitFile[2];
}
else if(i==3)
{
telNumber = splitFile[3];
}
else if(i==4)
{
emailAddress = splitFile[4];
}
else if(splitFile[i] == null)
{
System.out.println("You have failed!");
}
}
}
public static void deletionList()
{
File deletionEmails = new File("Testrun1.txt");
Scanner inputScanner1 = null;
String deletionfullName = null;
String deletionposition = null;
String deletioncompanyName= null;
String deletiontelNumber = null;
String deletionemailAddress = null;
try
{
inputScanner1 = new Scanner(deletionEmails);
}
catch(FileNotFoundException e)
{
System.out.println("File has not been found.");
}
while (inputScanner1.hasNextLine())
{
String deletionnextLine = inputScanner1.nextLine();
String [] deletionsplitFile = deletionnextLine.split(",");
for (int i = 0; i <deletionsplitFile.length;i++)
{
if(i==0)
{
deletionfullName = deletionsplitFile[0];
}
else if(i==1)
{
deletionposition = deletionsplitFile[1];
}
else if(i==2)
{
deletioncompanyName = deletionsplitFile[2];
}
else if(i==3)
{
deletiontelNumber = deletionsplitFile[3];
}
else if(i==4)
{
deletionemailAddress = deletionsplitFile[4];
}
else if(deletionsplitFile[i] == null)
{
System.out.println("You have failed!");
}
}
}
}
What I am trying to do is to take the fullName, emailAddress from the first split and deletionfullName and deletionemailAddress from the second split and compare the first and second of each, respectively. Each file will have a number of fields in it, and I am only interested in the fullName and emailAddress fields.
It is quite confusing to understand how you are trying to implement your solution, so may I suggest you look at a different way of doing the whole read-and-compare process. For example, I would suggest doing something like this... (in psuedocode)
public void compareFiles(String file1, String file2){
// Read the lines of each file into String[] arrays
String[] file1Lines = readAndSplitIntoLines(file1);
String[] file2Lines = readAndSplitIntoLines(file2);
// compare the lines
for (int x=0;x<file1Lines.length;x++){
for (int y=0;y<file2Lines.length;y++){
if (file1Lines[x].equals(file2Lines[y])){
// match. set it to null
file1Lines[x] = null;
file2Lines[y] = null;
// break out of the inner loop and start comparing the next line
break;
}
}
// remove the duplicates (which are now null values), creating a smaller array of uniques.
String[] newFile1 = shrinkArrayByRemovingNulls(file1Lines);
String[] newFile2 = shrinkArrayByRemovingNulls(file2Lines);
}
Besides the fact that your question is not very clear, you have at least one glaring problem:
DO NOT use exception handling for logic! Exception handling should be only for exceptions.
Secondly, think about what you are really looking to do. In pseudocode it would look something like this:
list1 = split(file(name1).read())
list2 = split(file(name2).read())
list3 = unique(list1, list2)
What does your code look like?

Categories

Resources