I need help with my code for computers, our final assignment is to make a simple game, so I decided to do Hangman. This assignment is worth 20% of my final mark due on Monday so if anybody could help me with it I would really appreciate it. (I'm still in high school and its just basic programming, so the code doesn't really need to be fancy as long as it works properly). The only thing I got to work were the lines, then everything else is just a huge mess.

class hangMan
{
  public static void main (String[] args)
  {
    String s = secretWord();
    int chances = 10;
    for (chances = 10; chances <= 10;)
    {
      System.out.println("The secret word is: " );
      printLines(" _ ",s.length());
      System.out.println("Enter a letter");
      char letter = In.getChar();
      
      letterChecking();
      
      if(letterChecking()==true)
      {
        letterSwap([letter]);
      }else
      {
        chances--;
        System.out.println("You have " + chances + " left.");
      }
    }
    
    if (chances > 0)
    {
      System.out.println("Congratulations the secret word is " + secretWord());
    }else
    {
      System.out.println("Sorry you lose, the secret word is " + secretWord());
    }
  }
  
  
  public static String secretWord()
  {
    System.out.println("What is the secret word");
    String secretWord = In.getString();
    return secretWord;
  }
  
  public static void printLines(String symbole, int width)
  {
    String s = secretWord();
    for (int w = 1; w <= s.length(); w++)
    {
      System.out.print(symbole);
    }
    System.out.println();
  }
  
  
  public static boolean letterChecking()
  {
    String s = secretWord();
    char letter;
    s.equalsIgnoreCase(secretWord());
    for (s.charAt(letter);;)
    {
      if (s.charAt(letter)== s.length())
      {
        return true;
      }else
      {
      }
    }
  }
  public static void letterSwap(char [] String)
  {
    int location = -1;
    String s = secretWord();
    char letter;
    char temp;
    for(char i; i < s.charAt(letter); i++)
    {
      if (String [letter] == s.charAt(letter))
      {
        location = s.charAt(letter);
        {
          if (i >= 0)
          {
            temp = String[i-1];
            String[i-1] = String[i];
            String[i] = temp;
          }
          System.out.println("The secret word is: " );
          printLines(" _ ",s.length());
        }
      }
    }
  }
}

You're going to need to expand on "just a huge mess". Post specific questions, error messages and stack traces, etc.

Please,say which line causes error in your program

Here are the problems that are immediately visible to me:

  • In is not defined anywhere in your program. Presumably it's intended to be something that can access the console input.
  • letter has no value inside letterChecking. You probably want to make it a parameter instead of a local variable.
  • letterSwap([letter]) doesn't need the square brackets.
  • i is not initialized in the for loop in letterSwap.

I fixed some of the problems in class today so I narrowed it down to 1 now. At line 11 and I don't konw what to do to fix it.

class hangMan
{
  public static void main (String[] args)
  {
    String s = secretWord();
    int chances = 10;
    while (chances < 11)
    {
      letterGuessing();
      
      if (letterSearch(s) == true)
      {
        
        
      }else
      {
        chances--;
      }
      
      System.out.println("You have " + chances + " left.");
    }
    
    if (chances > 0)
    {
      System.out.println("Congratulations the secret word is " + secretWord());
    }else
    {
      System.out.println("Sorry you lose, the secret word is " + secretWord());
    }
  }
  
  public static String secretWord()
    
  {
    System.out.println("What is the secret word");
    String secretWord = In.getString();
    return secretWord;
  }
  
  
  public static void printLines(String symbole, int width)
  {
    String s = secretWord();
    for (int w = 1; w <= s.length(); w++)
    {
      System.out.print( symbole );
    }
  }
  
  public static boolean letterSearch(char [] String)
  {
    char letter = letterGuessing();
    char item = letterGuessing();
    String s = secretWord();
    boolean found = false;
    for(s.charAt(letterGuessing());;)
    {
      if (String[letter]==item)
      {
        found = true;
      }
    }
  }
  
  public static char letterGuessing()
  {
    String s = secretWord();
    System.out.println("The secret word is: " );
    printLines(" _ ",s.length());
    System.out.println("Enter a letter");
    char letter = In.getChar();
    s.equalsIgnoreCase(secretWord());
    return letter;
  }
  
  public static String letterSwap(char [] String, char item)
  {
    String s = secretWord();
    char temp;
    for(s.charAt(letterGuessing());;)
    {
      if (String[letterGuessing()] == item)
      {
        temp = String[s.charAt(letterGuessing())];
        String[s.charAt(letterGuessing())] = String[letterGuessing()];
      }
    }
  }
}

you're line 56 is messed up, that's likely why you get a complaint for line 11 as it refers to the method letterSearch(), which contains the line 56. check that line and correct as needed.

I managed to fix a few more problems but now I need help with the following statements:
line 17: if the word isn't solved
line 71: if the letter being guessed is found in the word
line 75: while the word isn't solved yet
and is there a way to fix up my letterSwap method so that it does the following: if the letter being guessed is found in the word it changes the space " _ " with the letter

class hangMan
{
  public static void main (String[] args)
  {
    String s = secretWord();
    int chances = 10;
    while (chances < 11)
    {
      letterGuessing();
      
      if (letterSearch() == true)
      {
        while (chances < 11)
        {
          letterGuessing();
        }
        if ()
        {
          char [] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z'};
          
          letterSwap(letters, letterGuessing());
        }else
        {
          chances--;
          System.out.println("You have " + chances + " left.");
        }
      }else 
      {
        if (chances > 0)
        {
          System.out.println("Congratulations the secret word is " + secretWord());
        }else
        {
          System.out.println("Sorry you lose, the secret word is " + secretWord());
        }
      }
    }
  }
  
  
  
  public static String secretWord()
    
  {
    System.out.println("What is the secret word");
    String secretWord = In.getString();
    return secretWord;
  }
  
  
  public static void printLines(String symbole, int width)
  {
    String s = secretWord();
    for (int w = 1; w <= s.length(); w++)
    {
      System.out.print( symbole );
    }
  }
  
  public static boolean letterSearch()
  {
    char letter = letterGuessing();
    char item = letterGuessing();
    String s = secretWord();
    boolean found = false;
    do
    {
      char [] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z'};
      if ()
      {
        found = true;
      }
    }while(isSolved() = false);
  }
  
  public static char letterGuessing()
  {
    String s = secretWord();
    System.out.println("The secret word is: " );
    printLines(" _ ",s.length());
    System.out.println("Enter a letter");
    char letter = In.getChar();
    s.equalsIgnoreCase(secretWord());
    return letter;
  }
  
  public static String letterSwap(char [] String, char item)
  {
    String s = secretWord();
    char temp;
    do
    {
      if (String[letterGuessing()] == item)
      {
        temp = String[s.charAt(letterGuessing())];
        String[s.charAt(letterGuessing())] = String[letterGuessing()];
      }
    }while (isSolved() = false);
  }
  
  public static boolean isSolved()
  {
    String s = secretWord();
    int unknown = s.length();
    boolean solved = false;
    if (unknown < s.length())
    {
      solved = true;
    }
    return true;
  }
}

I fixed a few more problems but now the biggest problem with my code is the letterSwap method I need it to swap the space "_" with the letter being guess and filling in the statements at:
line 17: if the word isn't solved
line 71: if the letter being guessed is found in the word
line 75: while the word isn't solved yet

class hangMan
{
  public static void main (String[] args)
  {
    String s = secretWord();
    int chances = 10;
    while (chances < 11)
    {
      letterGuessing();
      
      if (letterSearch() == true)
      {
        while (chances < 11)
        {
          letterGuessing();
        }
        if ()
        {
          letterSwap();
        }else
        {
          chances--;
          System.out.println("You have " + chances + " left.");
        }
      }else 
      {
        if (chances > 0)
        {
          System.out.println("Congratulations the secret word is " + secretWord());
        }else
        {
          System.out.println("Sorry you lose, the secret word is " + secretWord());
        }
      }
    }
  }
  
  
  
  public static String secretWord()
    
  {
    System.out.println("What is the secret word");
    String secretWord = In.getString();
    return secretWord;
  }
  
  
  public static void printLines(String symbole, int width)
  {
    String s = secretWord();
    for (int w = 1; w <= s.length(); w++)
    {
      System.out.print( symbole );
    }
  }
  
  public static boolean letterSearch()
  {
    char letter = letterGuessing();
    char item = letterGuessing();
    String s = secretWord();
    boolean found = false;
    do
    {
      if ()
      {
        found = true;
      }
    }while(isSolved() = false);
  }
  
  public static char letterGuessing()
  {
    String s = secretWord();
    System.out.println("The secret word is: " );
    printLines(" _ ",s.length());
    System.out.println("Enter a letter");
    char letter = In.getChar();
    s.equalsIgnoreCase(secretWord());
    return letter;
  }
  
  public static String letterSwap()
  {
    String s = secretWord();
    char [] String;
    char item;
    char temp;
    do
    {
      if (String[letterGuessing()] == item)
      {
        temp = String[s.charAt(letterGuessing())];
        String[s.charAt(letterGuessing())] = String[letterGuessing()];
      }
    }while (isSolved() == false);
  return s;
  }
  
  public static boolean isSolved()
  {
    String s = secretWord();
    int unknown = s.length();
    boolean solved = false;
    if (unknown < s.length())
    {
      solved = true;
    }
    return true;
  }
}

I worked everything out exept the letterSwap(starts at Line 75) method any suggestions on how to make it work.

class hangMan
{
  public static void main (String[] args)
  {
    int chances = 10;
    while (chances <= 10)
    {
      letterGuessing();
      if (letterSearch() == true)
      {
        while (chances <= 10)
        {
          letterSwap();
        }
      }else
        {
          chances--;
          System.out.println("You have " + chances + " left.");
        }
      }
      
      if (chances > 0)
      {
        System.out.println("Congratulations the secret word is " + secretWord());
      }else
      {
        System.out.println("Sorry you lose, the secret word is " + secretWord());
      }
    }
    
    
    
    public static String secretWord()
      
    {
      System.out.println("What is the secret word");
      String secretWord = In.getString();
      return secretWord;
    }
    
    
    public static void printLines(String symbole, int width)
    {
      String s = secretWord();
      for (int w = 1; w <= s.length(); w++)
      {
        System.out.print( symbole );
      }
    }
    
    public static boolean letterSearch()
    {
      boolean found = false;
      do
      {
        if (isSolved() == false)
        {
          found = true;
        }
      }while(isSolved() == false);
      return true;
    }
    
    public static char letterGuessing()
    {
      String s = secretWord();
      System.out.println("The secret word is: " );
      printLines(" _ ",s.length());
      System.out.println("Enter a letter");
      char letter = In.getChar();
      s.equalsIgnoreCase(secretWord());
      return letter;
    }
    
    public static String letterSwap()
    {
      String s = secretWord();
      char [] String;
      char temp;
      temp = String[s.charAt(letterGuessing())];
      String[s.charAt(letterGuessing())] = String[letterGuessing()];
      return s;
    }
    
    public static boolean isSolved()
    {
      String s = secretWord();
      int unknown = s.length();
      boolean solved = false;
      if (unknown == s.length())
      {
        solved = true;
      }
      return true;
    }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.