Jump to content

Need some help with Java


Kobold

Recommended Posts

Line 22 gives me an error that says "Illegal start of expression."
Line 343 gives me an error that says "main(java.lang.String[]) is already defined in hangman.HangMan"

I need help getting rid of these 2 errors so this will work properly.

[spoiler="Code"]
[code]
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hangman;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author Kobold
*/
public class HangMan {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
public static ArrayList<String> wordList;
public static String word;
public static String guess;
public static int numGuesses = 0;
public static Scanner scan = new Scanner(System.in);

public static final int MAX_GUESSES = 6;
public static final String fileName = "mlpwords.txt";



//Directions!
public static void displayInstructions()
{
System.out.println("1. Pick a letter, any letter. If you are right, the letter will be filled in the blanks. "+
"If you are wrong, more of your body will be hanged. You will get six tries. If your body is"+
" fully hanged, you lose. Have fun and don't die.");

}


//Game Over!
public static boolean done()
{
if(lost() || won())
{
return true;
}
return false;

}

//A wonner is you?
public static boolean won()
{
if(guess.equals(word))
{
return true;
}

return false;
}

//You lose.
public static boolean lost()
{
if(numGuesses == MAX_GUESSES)
{
return true;
}
else
{
return false;
}

}

//Hit?
public static boolean hasLetter(String guess, String word)
{

if(word.indexOf(guess) != -1)
{
return true;
}
return false;


}

//You sunk my battleship
public static void addGuess(String letter)
{
int index = 0;
int len = guess.length();

for(int i = 0; i < len; i++)
{
index = word.indexOf(letter, index);

if (index != -1)
{
String start = guess.substring(0, index);
String end = guess.substring((index + 1), guess.length());
guess = start + letter + end;
index++;
}
}
}

//Come at me bro.
public static String getGuess()
{
System.out.print("Pick a letter, any letter: ");
return scan.next();
}

//Generate Playing Field
public static String setEmptyString(int size)
{
size = word.length();
guess ="";

for(int count = 0; count < size; count++)
{
guess += "-";
}

return guess;
}

//Selecting a word
public static String chooseWord(ArrayList<String> list)
{
int index = (int)(Math.random() * list.size());

return list.get(index);
}

//The Good Part
public static ArrayList<String> getWords(String fileName) throws FileNotFoundException
{
Scanner file = new Scanner(new File(fileName));

ArrayList<String> words = new ArrayList<String>();

while(file.hasNextLine())
{
words.add(file.nextLine());
}

return words;
}

//Its dangerous to go alone...
public static void drawGallows()
{
//Lets add some good old violence.
String top =
"...............................\n"+
"| .........................|\n"+
"| / | ";

String head =
"| | ___ \n"+
"| | /x x\\ \n"+
"| | \\# / \n"+
"| | = ";

String noHead =
"| | \n"+
"| | \n"+
"| | \n"+
"| | ";

String body =
"| | | \n"+
"| | | \n"+
"| | | \n"+
"| | | ";

String noBody =
"| | \n"+
"| | \n"+
"| | \n"+
"| | ";

String arm =
"| | /==| \n"+
"| | / | ";

String arms =
"| | /==|==\\ \n"+
"| | / | \\ ";

String noArms =
"| | \n"+
"| | ";
String leg =
"| | / \n"+
"| | / \n"+
"| | _/ ";

String bothLegs =
"| | / \\ \n"+
"| | / \\ \n"+
"| | _/ \\_ ";

String noLegs =
"| | \n"+
"| | \n"+
"| | ";

String bottom =
"| | \n"+
"| | \n"+
"| \\ \n"+
"------------^^^^^^^^^^^^^^^---- ";


//...Take these gallows.
switch(numGuesses)
{
case 0:
{
System.out.println(top);
System.out.println(noHead);
System.out.println(noArms);
System.out.println(noBody);
System.out.println(noLegs);
System.out.println(bottom);
break;
}
case 1:
{
System.out.println(top);
System.out.println(head);
System.out.println(noArms);
System.out.println(noBody);
System.out.println(noLegs);
System.out.println(bottom);
break;
}
case 2:
{
System.out.println(top);
System.out.println(head);
System.out.println(arm);
System.out.println(noBody);
System.out.println(noLegs);
System.out.println(bottom);
break;
}
case 3:
{
System.out.println(top);
System.out.println(head);
System.out.println(arms);
System.out.println(noBody);
System.out.println(noLegs);
System.out.println(bottom);
break;
}
case 4:
{
System.out.println(top);
System.out.println(head);
System.out.println(arms);
System.out.println(body);
System.out.println(noLegs);
System.out.println(bottom);
break;
}
case 5:
{
System.out.println(top);
System.out.println(head);
System.out.println(arms);
System.out.println(body);
System.out.println(leg);
System.out.println(bottom);
break;
}
case 6:
{
System.out.println(top);
System.out.println(head);
System.out.println(arms);
System.out.println(body);
System.out.println(bothLegs);
System.out.println(bottom);
break;
}
}

}


//Lets play the game.
public static void Game()
{
String letterList = "";
String letter;
while(!done())
{
System.out.println(guess);
if(letterList.length() > 0)
{
System.out.println("You have used these letters: " + letterList);
}

letter = getGuess();

letterList += letter + " ";

if(letter.equals(word))
{
break;
}
else if(letter.length() > 1)
{
System.out.println("Wrong");
numGuesses++;
}
else
{
if(hasLetter(letter, word))
{
addGuess(letter);
}
else
{
numGuesses++;
System.out.println("Wrong!");
}
}
drawGallows();

}
}

public static void main(String[] args) throws FileNotFoundException
{
boolean playAgain = true;
String userInput;
while(playAgain)
{
//Pressed Start and Playing Again
displayInstructions();
System.out.println("");
System.out.println("Here is your word template");
wordList = getWords(fileName);
word = chooseWord(wordList);
guess = setEmptyString(word.length());

Game();

if(lost())
{
System.out.println("Well... Better luck next time..."
+ ".<||{GAME OVER!}||>.");

}
else
{
System.out.println("");
System.out.println("A wonner is you!");
System.out.println("The word was " + word);
System.out.println("\n\n ^__^\n"+
" (oo) < {Moo!!!}\n"+
" /-----(__)\n"+
" / | || \n"+
" * /\\---/\\ \n"+
" ~~ ~~ \n");
}


boolean validInput = false;
while(!validInput)
{
System.out.print("Want to play again? (y or n) : ");
userInput = scan.next();
if(userInput.equals("y"))
{
validInput = true;
playAgain = true;
numGuesses = 0;
}
if(userInput.equals("n"))
{
validInput = true;
playAgain = false;
}
}
}
}
}[/code]
[/spoiler]

I am using Netbeans 7.1 and I need help ASAP please. Thank you for your time.

Some of the ASKII drawing doesn't format properly on TCM apparently, and I apologize, but good news is that we don't need that part to fix this anyway.

Link to comment
Share on other sites

The first error is because you can't use modifiers like [i]public[/i] or [i]static[/i] for a variable inside a method. Get rid of those.

Second is because you have two methods called [i]main[/i].

[img]http://images.icanhascheezburger.com/completestore/2009/2/15/128792416036462748.jpg[/img]

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...