My program changes one letter of a 4 letter word and switches it with something else typed in, then updates
up vote
-1
down vote
favorite
I am a beginner programmer and am trying to figure out what is wrong with my code. How can I fix my mistakes? This is the code I could type out so far. This is pretty similar to the game "Word Ladder". I keep getting this error and cannot understand how to fix this:
OneLetterGame.java:67: error: incompatible types: possible lossy conversion from int to char: charArray[0] = index ;
Code:
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
public class OneLetterGame {
public static void main(String args) {
String startWord = "";
String currentWord = "";
String goalWord = "";
String error = "";
char newLetter = '';
int index = 0;
int steps = 0;
do {
String fileContents = getFileContents("dictionary.txt");
startWord = JOptionPane.showInputDialog(error + "Player One, please enter a FOUR letter word: ");
if (startWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
currentWord = startWord;
/* for(int i = 0; i < fileContents.length;i++){
if(currentWord.equals(fileContents[i]){
break;
}else{
error = "This is not a valid four letter word. Please try again. n";
continue;
}
}//for */
System.out.println(startWord);
goalWord = JOptionPane.showInputDialog("Player Two, please enter a FOUR letter GOAL word: ");
if (goalWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
steps++;
//user enters step has form index space letter
currentWord = JOptionPane.showInputDialog("Please enter the letter you want to change and its location (ex: 1 a) n The Goal Word is " + goalWord + " :");
if (currentWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
//pulls out character at index 0
index = currentWord.charAt((int) 0 - 48);
//pulls out character at index 3
newLetter = currentWord.charAt(3);
//string to char
char charArray;
charArray = currentWord.toCharArray();
charArray[0] = index;
charArray[3] = newLetter;
currentWord = String.valueOf(charArray);
//char to string
currentWord = new String(charArray);
System.out.println(currentWord);
if (currentWord.equals(goalWord)) {
if (steps % 2 == 0) {
System.out.println("Congratulations to Player2. You won in " + steps + " step(s)!");
} else {
System.out.println("Congratulations to Player1. You won in " + steps + " step(s)!");
}
break;
} else {
continue;
}
} while (true);
} //main
public static String getFileContents(String fileName) {
String contents = null;
int length = 0;
int num = 0;
try {
// input
String folderName = "/subFolder/"; // if the file is contained in the same folder as the .class file, make this equal to the empty string
String resource = fileName;
// this is the path within the jar file
InputStream input = OneLetterGame.class.getResourceAsStream(folderName + resource);
if (input == null) {
// this is how we load file within editor (eg eclipse)
input = OneLetterGame.class.getClassLoader().getResourceAsStream(resource);
}
BufferedReader in = new BufferedReader(new InputStreamReader(input));
in .mark(Short.MAX_VALUE); // see api
// count number of lines in file
while ( in .readLine() != null) {
length++;
}
in .reset(); // rewind the reader to the start of file
contents = new String[length]; // give size to contents array
// read in contents of file and print to screen
for (int i = 0; i < length; i++) {
contents[i] = in .readLine();
} in .close();
} catch (Exception e) {
System.out.println("File Input Error");
}
return contents;
} // getFileContents
}//onelettergame
java
add a comment |
up vote
-1
down vote
favorite
I am a beginner programmer and am trying to figure out what is wrong with my code. How can I fix my mistakes? This is the code I could type out so far. This is pretty similar to the game "Word Ladder". I keep getting this error and cannot understand how to fix this:
OneLetterGame.java:67: error: incompatible types: possible lossy conversion from int to char: charArray[0] = index ;
Code:
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
public class OneLetterGame {
public static void main(String args) {
String startWord = "";
String currentWord = "";
String goalWord = "";
String error = "";
char newLetter = '';
int index = 0;
int steps = 0;
do {
String fileContents = getFileContents("dictionary.txt");
startWord = JOptionPane.showInputDialog(error + "Player One, please enter a FOUR letter word: ");
if (startWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
currentWord = startWord;
/* for(int i = 0; i < fileContents.length;i++){
if(currentWord.equals(fileContents[i]){
break;
}else{
error = "This is not a valid four letter word. Please try again. n";
continue;
}
}//for */
System.out.println(startWord);
goalWord = JOptionPane.showInputDialog("Player Two, please enter a FOUR letter GOAL word: ");
if (goalWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
steps++;
//user enters step has form index space letter
currentWord = JOptionPane.showInputDialog("Please enter the letter you want to change and its location (ex: 1 a) n The Goal Word is " + goalWord + " :");
if (currentWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
//pulls out character at index 0
index = currentWord.charAt((int) 0 - 48);
//pulls out character at index 3
newLetter = currentWord.charAt(3);
//string to char
char charArray;
charArray = currentWord.toCharArray();
charArray[0] = index;
charArray[3] = newLetter;
currentWord = String.valueOf(charArray);
//char to string
currentWord = new String(charArray);
System.out.println(currentWord);
if (currentWord.equals(goalWord)) {
if (steps % 2 == 0) {
System.out.println("Congratulations to Player2. You won in " + steps + " step(s)!");
} else {
System.out.println("Congratulations to Player1. You won in " + steps + " step(s)!");
}
break;
} else {
continue;
}
} while (true);
} //main
public static String getFileContents(String fileName) {
String contents = null;
int length = 0;
int num = 0;
try {
// input
String folderName = "/subFolder/"; // if the file is contained in the same folder as the .class file, make this equal to the empty string
String resource = fileName;
// this is the path within the jar file
InputStream input = OneLetterGame.class.getResourceAsStream(folderName + resource);
if (input == null) {
// this is how we load file within editor (eg eclipse)
input = OneLetterGame.class.getClassLoader().getResourceAsStream(resource);
}
BufferedReader in = new BufferedReader(new InputStreamReader(input));
in .mark(Short.MAX_VALUE); // see api
// count number of lines in file
while ( in .readLine() != null) {
length++;
}
in .reset(); // rewind the reader to the start of file
contents = new String[length]; // give size to contents array
// read in contents of file and print to screen
for (int i = 0; i < length; i++) {
contents[i] = in .readLine();
} in .close();
} catch (Exception e) {
System.out.println("File Input Error");
}
return contents;
} // getFileContents
}//onelettergame
java
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am a beginner programmer and am trying to figure out what is wrong with my code. How can I fix my mistakes? This is the code I could type out so far. This is pretty similar to the game "Word Ladder". I keep getting this error and cannot understand how to fix this:
OneLetterGame.java:67: error: incompatible types: possible lossy conversion from int to char: charArray[0] = index ;
Code:
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
public class OneLetterGame {
public static void main(String args) {
String startWord = "";
String currentWord = "";
String goalWord = "";
String error = "";
char newLetter = '';
int index = 0;
int steps = 0;
do {
String fileContents = getFileContents("dictionary.txt");
startWord = JOptionPane.showInputDialog(error + "Player One, please enter a FOUR letter word: ");
if (startWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
currentWord = startWord;
/* for(int i = 0; i < fileContents.length;i++){
if(currentWord.equals(fileContents[i]){
break;
}else{
error = "This is not a valid four letter word. Please try again. n";
continue;
}
}//for */
System.out.println(startWord);
goalWord = JOptionPane.showInputDialog("Player Two, please enter a FOUR letter GOAL word: ");
if (goalWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
steps++;
//user enters step has form index space letter
currentWord = JOptionPane.showInputDialog("Please enter the letter you want to change and its location (ex: 1 a) n The Goal Word is " + goalWord + " :");
if (currentWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
//pulls out character at index 0
index = currentWord.charAt((int) 0 - 48);
//pulls out character at index 3
newLetter = currentWord.charAt(3);
//string to char
char charArray;
charArray = currentWord.toCharArray();
charArray[0] = index;
charArray[3] = newLetter;
currentWord = String.valueOf(charArray);
//char to string
currentWord = new String(charArray);
System.out.println(currentWord);
if (currentWord.equals(goalWord)) {
if (steps % 2 == 0) {
System.out.println("Congratulations to Player2. You won in " + steps + " step(s)!");
} else {
System.out.println("Congratulations to Player1. You won in " + steps + " step(s)!");
}
break;
} else {
continue;
}
} while (true);
} //main
public static String getFileContents(String fileName) {
String contents = null;
int length = 0;
int num = 0;
try {
// input
String folderName = "/subFolder/"; // if the file is contained in the same folder as the .class file, make this equal to the empty string
String resource = fileName;
// this is the path within the jar file
InputStream input = OneLetterGame.class.getResourceAsStream(folderName + resource);
if (input == null) {
// this is how we load file within editor (eg eclipse)
input = OneLetterGame.class.getClassLoader().getResourceAsStream(resource);
}
BufferedReader in = new BufferedReader(new InputStreamReader(input));
in .mark(Short.MAX_VALUE); // see api
// count number of lines in file
while ( in .readLine() != null) {
length++;
}
in .reset(); // rewind the reader to the start of file
contents = new String[length]; // give size to contents array
// read in contents of file and print to screen
for (int i = 0; i < length; i++) {
contents[i] = in .readLine();
} in .close();
} catch (Exception e) {
System.out.println("File Input Error");
}
return contents;
} // getFileContents
}//onelettergame
java
I am a beginner programmer and am trying to figure out what is wrong with my code. How can I fix my mistakes? This is the code I could type out so far. This is pretty similar to the game "Word Ladder". I keep getting this error and cannot understand how to fix this:
OneLetterGame.java:67: error: incompatible types: possible lossy conversion from int to char: charArray[0] = index ;
Code:
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
public class OneLetterGame {
public static void main(String args) {
String startWord = "";
String currentWord = "";
String goalWord = "";
String error = "";
char newLetter = '';
int index = 0;
int steps = 0;
do {
String fileContents = getFileContents("dictionary.txt");
startWord = JOptionPane.showInputDialog(error + "Player One, please enter a FOUR letter word: ");
if (startWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
currentWord = startWord;
/* for(int i = 0; i < fileContents.length;i++){
if(currentWord.equals(fileContents[i]){
break;
}else{
error = "This is not a valid four letter word. Please try again. n";
continue;
}
}//for */
System.out.println(startWord);
goalWord = JOptionPane.showInputDialog("Player Two, please enter a FOUR letter GOAL word: ");
if (goalWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
steps++;
//user enters step has form index space letter
currentWord = JOptionPane.showInputDialog("Please enter the letter you want to change and its location (ex: 1 a) n The Goal Word is " + goalWord + " :");
if (currentWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if
//pulls out character at index 0
index = currentWord.charAt((int) 0 - 48);
//pulls out character at index 3
newLetter = currentWord.charAt(3);
//string to char
char charArray;
charArray = currentWord.toCharArray();
charArray[0] = index;
charArray[3] = newLetter;
currentWord = String.valueOf(charArray);
//char to string
currentWord = new String(charArray);
System.out.println(currentWord);
if (currentWord.equals(goalWord)) {
if (steps % 2 == 0) {
System.out.println("Congratulations to Player2. You won in " + steps + " step(s)!");
} else {
System.out.println("Congratulations to Player1. You won in " + steps + " step(s)!");
}
break;
} else {
continue;
}
} while (true);
} //main
public static String getFileContents(String fileName) {
String contents = null;
int length = 0;
int num = 0;
try {
// input
String folderName = "/subFolder/"; // if the file is contained in the same folder as the .class file, make this equal to the empty string
String resource = fileName;
// this is the path within the jar file
InputStream input = OneLetterGame.class.getResourceAsStream(folderName + resource);
if (input == null) {
// this is how we load file within editor (eg eclipse)
input = OneLetterGame.class.getClassLoader().getResourceAsStream(resource);
}
BufferedReader in = new BufferedReader(new InputStreamReader(input));
in .mark(Short.MAX_VALUE); // see api
// count number of lines in file
while ( in .readLine() != null) {
length++;
}
in .reset(); // rewind the reader to the start of file
contents = new String[length]; // give size to contents array
// read in contents of file and print to screen
for (int i = 0; i < length; i++) {
contents[i] = in .readLine();
} in .close();
} catch (Exception e) {
System.out.println("File Input Error");
}
return contents;
} // getFileContents
}//onelettergame
java
java
edited Nov 10 at 11:17
halfer
14.2k757106
14.2k757106
asked Nov 8 at 3:59
Tanish Upreti
33
33
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
This is similar to this answer
You are trying to assign a char
type to an int
value. Try casting to type char like this instead:
charArray[0] = (char) index;
add a comment |
up vote
1
down vote
You need to do casting properly use --> charArray[0] = (char) index;
Although if you are doing this in some IDE then compilation error will shown something like this --> Type mismatch: cannot convert from int to char
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
This is similar to this answer
You are trying to assign a char
type to an int
value. Try casting to type char like this instead:
charArray[0] = (char) index;
add a comment |
up vote
0
down vote
accepted
This is similar to this answer
You are trying to assign a char
type to an int
value. Try casting to type char like this instead:
charArray[0] = (char) index;
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
This is similar to this answer
You are trying to assign a char
type to an int
value. Try casting to type char like this instead:
charArray[0] = (char) index;
This is similar to this answer
You are trying to assign a char
type to an int
value. Try casting to type char like this instead:
charArray[0] = (char) index;
answered Nov 8 at 4:06
rileyjsumner
1871115
1871115
add a comment |
add a comment |
up vote
1
down vote
You need to do casting properly use --> charArray[0] = (char) index;
Although if you are doing this in some IDE then compilation error will shown something like this --> Type mismatch: cannot convert from int to char
add a comment |
up vote
1
down vote
You need to do casting properly use --> charArray[0] = (char) index;
Although if you are doing this in some IDE then compilation error will shown something like this --> Type mismatch: cannot convert from int to char
add a comment |
up vote
1
down vote
up vote
1
down vote
You need to do casting properly use --> charArray[0] = (char) index;
Although if you are doing this in some IDE then compilation error will shown something like this --> Type mismatch: cannot convert from int to char
You need to do casting properly use --> charArray[0] = (char) index;
Although if you are doing this in some IDE then compilation error will shown something like this --> Type mismatch: cannot convert from int to char
answered Nov 8 at 5:35
Amit
376111
376111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201367%2fmy-program-changes-one-letter-of-a-4-letter-word-and-switches-it-with-something%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown