Jump to content

Java help

Go to solution Solved by 79wjd,

Could you help me fix that error?

string input = scan.next(); 

char guessedLetter = input.charAt(0);

Hey guys. So my I'm working on a project with a partner. She wrote this code and asked me to take a look at it. When I try to compile it (this is a small portion of a bigger project) it highlights the bold text and says "unexpected type    required: variable; found: value" Can anyone tell me what's going on and how to fix it? I would really appreciate it. Oh and forget all of those brackets at the end there. They are from the earlier parts of the project.

 

public static void letterReplacement (String displayedWord)
    {
        Scanner scan = new Scanner(System.in);
        StringBuilder displayedWord2 = new StringBuilder(displayedWord);
        int lengthOfDisplayedWord = displayedWord2.length();
        int lostGames;
        int guessesLeft = 5;
        int count = 0;

       if (guessesLeft == 0)
       {
            lostGames++;
       }

       else
       {
        for(guessesLeft = 5; guessesLeft <= 0; guessesLeft--)
        {
         for(count=0; count < lengthOfDisplayedWord; count ++)
         {
            System.out.println(displayedWord);
            System.out.println("Guess a letter: ");
            String guessedLetter = scan.next();
           
            if (Character.isWhitespace(displayedWord2.charAt(count)))
            {

               

            }
            else
            {
              if (Character.isLetter(displayedWord2.charAt(count)))
              {

                 

              }
              else
              {
                  if (displayedWord.charAt(count) = (guessedLetter))
                  {

                     

                  }
                  else
                  {

                     

                  }
              }
            }
         }
        }
       }
    }

Link to comment
https://linustechtips.com/topic/262106-java-help/
Share on other sites

Link to post
Share on other sites

I reformatted the spacing to make it actually readable, and it doesn't do anything. Whats the code supposed to do? The line it fails on needs to be == not = since its a comparison not an assignment. 

public static void letterReplacement (String displayedWord)    {        Scanner scan = new Scanner(System.in);        StringBuilder displayedWord2 = new StringBuilder(displayedWord);        int lengthOfDisplayedWord = displayedWord2.length();        int lostGames;        int guessesLeft = 5;        int count = 0;       if (guessesLeft == 0)       {            lostGames++;       }       else       {            for(guessesLeft = 5; guessesLeft <= 0; guessesLeft--)            {                  for(count=0; count < lengthOfDisplayedWord; count ++)                   {                        System.out.println(displayedWord);                        System.out.println("Guess a letter: ");                       String guessedLetter = scan.next();                         if (Character.isWhitespace(displayedWord2.charAt(count)))                         {                         //does nothing                          }                          else                          {                                   if (Character.isLetter(displayedWord2.charAt(count)))                                  {                                    //does nothing                                   }                                    else                                   {                                            if (displayedWord.charAt(count) == (guessedLetter))                                            {                                              //does nothing                                             }                                             else                                             {                                                     //does nothing                                             }                                     }            }         }        }       }    }

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568260
Share on other sites

Link to post
Share on other sites

 

I reformatted the spacing to make it actually readable, and it doesn't do anything. Whats the code supposed to do, and does it tell you what line it fails on? 

public static void letterReplacement (String displayedWord)    {        Scanner scan = new Scanner(System.in);        StringBuilder displayedWord2 = new StringBuilder(displayedWord);        int lengthOfDisplayedWord = displayedWord2.length();        int lostGames;        int guessesLeft = 5;        int count = 0;       if (guessesLeft == 0)       {            lostGames++;       }       else       {            for(guessesLeft = 5; guessesLeft <= 0; guessesLeft--)            {                  for(count=0; count < lengthOfDisplayedWord; count ++)                   {                        System.out.println(displayedWord);                        System.out.println("Guess a letter: ");                       String guessedLetter = scan.next();                         if (Character.isWhitespace(displayedWord2.charAt(count)))                         {                         //does nothing                          }                          else                          {                                   if (Character.isLetter(displayedWord2.charAt(count)))                                  {                                                    }                                    else                                   {                                            if (displayedWord.charAt(count) = (guessedLetter))                                            {                                                                  }                                             else                                             {                                                                  }                                     }            }         }        }       }    }

Sorry about that, I blame it on BlueJ (my teacher wants us using that) And it fails on the very last if statement. Oh and it plays the game hangman. So it has a dictionary to 20 words and displays spaces, then the user has 6 guesses to get the word. The user guesses letters and the program replaces the spaces with the letters if they are correct.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568279
Share on other sites

Link to post
Share on other sites

Sorry about that, I blame it on BlueJ (my teacher wants us using that) And it fails on the very last if statement.

Whats the code supposed to do? The line it fails on needs to be == not = since its a comparison not an assignment. 

Also, the for loop condition should be > 0, not <= 0. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568287
Share on other sites

Link to post
Share on other sites

Whats the code supposed to do? The line it fails on needs to be == not = since its a comparison not an assignment. 

Also, the for loop condition should be > 0, not <= 0. 

Do you want me to give you the entire code so you can see? It's incomplete but you can at least read it.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568318
Share on other sites

Link to post
Share on other sites

Sorry about the spacing. I think it's my editor.

 

 

/*

* This class contains the main driver of the game

*

* Name: Nick Marino and Jessie Slater

* Date: 11-24-14

*/

import java.util.Random;

import java.util.Scanner;

public class HangMan

{

    private static int wordChoice;

    private static String displayedWord;

   

    public static String pickWord(int wordChoice)

    {

        switch(wordChoice)

        {

            case 0:

                displayedWord = "motherboard";

            case 1:

                displayedWord = "case";

            case 2:

                displayedWord = "powersupply";

            case 3:

                displayedWord = "harddrive";

            case 4:

                displayedWord = "mouse";

            case 5:

                displayedWord = "keyboard";

            case 6:

                displayedWord = "monitor";

            case 7:

                displayedWord = "graphicscard";

            case 8:

                displayedWord = "processor";

            case 9:

                displayedWord = "latency";

            case 10:

                displayedWord = "ping";

            case 11:

                displayedWord = "gaming";

            case 12:

                displayedWord = "resolution";

            case 13:

                displayedWord = "watercooling";

            case 14:

                displayedWord = "cache";

            case 15:

                displayedWord = "memory";

            case 16:

                displayedWord = "chipset";

            case 17:

                displayedWord = "intel";

            case 18:

                displayedWord = "notebook";

            case 19:

                displayedWord = "desktop";

            case 20:

                displayedWord = "aliasing";

        }

        return displayedWord;

    }
   
    public static void main(String[]args)
    {
            Scanner scan = new Scanner(System.in);
            String userChoice;
            int gameCount = 0;
            int lostGames = 0;

            System.out.print("Welcome to a game of hangman!");
            System.out.print("Do you want to play? (Y/N): ");
            userChoice = scan.next();

            while (!userChoice.equalsIgnoreCase("n"))
            {
                  switch (userChoice)
                 {
                       case "Y":
                       case "y":
                           Random wordPick = new Random();
                           System.out.print(pickWord(wordPick.nextInt()));
                           letterReplacement(displayedWord);
                           break;             

                       default:
                           System.out.println("Bad input.");
                           break;
                  }
                  gameCount++;
                  System.out.println("You have played " + gameCount + " games. Do you want to play again?");
                  userChoice = scan.next();

            } 
    }
   
    public static void letterReplacement (String displayedWord)
    {
        Scanner scan = new Scanner(System.in);
        StringBuilder displayedWord2 = new StringBuilder(displayedWord);
        int lengthOfDisplayedWord = displayedWord2.length();
        int lostGames;
        int guessesLeft = 5;
        int count = 0;

       if (guessesLeft == 0)
       {
            lostGames++;
       }

       else
       {
        for(guessesLeft = 5; guessesLeft <= 0; guessesLeft--)
        {
         for(count=0; count < lengthOfDisplayedWord; count ++)
         {
            System.out.println(displayedWord);
            System.out.println("Guess a letter: ");
            String guessedLetter = scan.next();
           
            if (Character.isWhitespace(displayedWord2.charAt(count)))
            {

               

            }
            else
            {
              if (Character.isLetter(displayedWord2.charAt(count)))
              {

                 

              }
              else
              {
                  if (displayedWord.charAt(count) == (guessedLetter))
                  {

                     

                  }
                  else
                  {

                     

                  }
              }
            }
         }
        }
       }
    }
}

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568331
Share on other sites

Link to post
Share on other sites

Do you want me to give you the entire code so you can see? It's incomplete but you can at least read it.

I'm just pointing out that the code you listed doesnt actually do anything other than deitterate 'guessesleft' and print a couple lines (the conditionals are all empty). 

 

But if you change the = to an == it should work (or at least compile). 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568335
Share on other sites

Link to post
Share on other sites

I'm just pointing out that the code you listed doesnt actually do anything other than deitterate 'guessesleft' and print a couple lines (the conditionals are all empty). 

 

But if you change the = to an == it should work (or at least compile). 

Yes I know, I said that it was part of a bigger project. But it says at that very last if statement  "unexpected type    required: variable; found: value" and it highlights the word "count"

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568352
Share on other sites

Link to post
Share on other sites

Yes I know, I said that it was part of a bigger project. But it says at that very last if statement  "unexpected type    required: variable; found: value" and it highlights the word "count"

shouldn't you be using displayedWord2.charAt(count) not displayedWord.charAt(count).

 

You should also change guessedLetter to type char instead of string. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568366
Share on other sites

Link to post
Share on other sites

shouldn't you be using displayedWord2.charAt(count). 

 

You should also change guessed letter to type char instead of string. 

Even when I change that it still has the same error. == doesn't work either. Can you put it through your editor and see for yourself?

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568378
Share on other sites

Link to post
Share on other sites

Even when I change that it still has the same error. == doesn't work either. Can you put it through your editor and see for yourself?

Did you change guessedLetter to type char 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568417
Share on other sites

Link to post
Share on other sites

How exactly do I do that? I was just trying to....

just do: 

char guessedLetter = //scanner; 
 

I'm pretty sure you can use a scanner for a char the same was as a string. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568450
Share on other sites

Link to post
Share on other sites

just do: 

char guessedLetter = //scanner; 
 

I'm pretty sure you can use a scanner for a char the same was as a string. 

char guessedLetter = scan.next(); 

How do you scan in a character though? When I compile this, it highlights the () and says that string cannot be converted to char.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568470
Share on other sites

Link to post
Share on other sites

 

I also get the error

 

Variable 'lostGames' might not have been initialized

// ...int lostGames; // not initializedint guessesLeft = 5;int count = 0;if (guessesLeft == 0){    lostGames++; // but trying to increment it}// ...

I'm still getting the original error

 

Hey guys. So my I'm working on a project with a partner. She wrote this code and asked me to take a look at it. When I try to compile it (this is a small portion of a bigger project) it highlights the bold text and says "unexpected type    required: variable; found: value" Can anyone tell me what's going on and how to fix it? I would really appreciate it. Oh and forget all of those brackets at the end there. They are from the earlier parts of the project.

 

public static void letterReplacement (String displayedWord)

    {

        Scanner scan = new Scanner(System.in);

        StringBuilder displayedWord2 = new StringBuilder(displayedWord);

        int lengthOfDisplayedWord = displayedWord2.length();

        int lostGames;

        int guessesLeft = 5;

        int count = 0;

       if (guessesLeft == 0)

       {

            lostGames++;

       }

       else

       {

        for(guessesLeft = 5; guessesLeft <= 0; guessesLeft--)

        {

         for(count=0; count < lengthOfDisplayedWord; count ++)

         {

            System.out.println(displayedWord);

            System.out.println("Guess a letter: ");

            String guessedLetter = scan.next();

           

            if (Character.isWhitespace(displayedWord2.charAt(count)))

            {

               

            }

            else

            {

              if (Character.isLetter(displayedWord2.charAt(count)))

              {

                 

              }

              else

              {

                  if (displayedWord.charAt(count) = (guessedLetter))

                  {

                     

                  }

                  else

                  {

                     

                  }

              }

            }

         }

        }

       }

    }

 

 

 

I also get the error

 

Variable 'lostGames' might not have been initialized

// ...int lostGames; // not initializedint guessesLeft = 5;int count = 0;if (guessesLeft == 0){    lostGames++; // but trying to increment it}// ...

I'm still getting the original error.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568531
Share on other sites

Link to post
Share on other sites

Could you help me fix that error?

string input = scan.next(); 

char guessedLetter = input.charAt(0);

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/262106-java-help/#findComment-3568613
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×