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









share|improve this question




























    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









    share|improve this question


























      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









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 11:17









      halfer

      14.2k757106




      14.2k757106










      asked Nov 8 at 3:59









      Tanish Upreti

      33




      33
























          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;





          share|improve this answer




























            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






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              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

























              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;





              share|improve this answer

























                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;





                share|improve this answer























                  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;





                  share|improve this answer












                  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;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 4:06









                  rileyjsumner

                  1871115




                  1871115
























                      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






                      share|improve this answer

























                        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






                        share|improve this answer























                          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






                          share|improve this answer












                          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







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 5:35









                          Amit

                          376111




                          376111






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              這個網誌中的熱門文章

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Xamarin.form Move up view when keyboard appear

                              JBPM : POST request for execute process go wrong