user3126549
user3126549

Reputation: 13

import java.util.Scanner is not valid,since it does not name a type in a package

im getting an error everytime i try to run my hangman code. Using ready to program IDE, if i hit run it just highlights import java.util.Scanner statement and tells me it is not valid.(import java.util.Scanner is not valid,since it does not name a type in a package. that is exactly what it tells me). I am new to this so i would appreciate if anyone could correct the code and just post it as a reply :) thanks.

package HangmanSummative;
    import  java.lang.System.out;
    import java.util.Scanner;

class Game
{
    public static void main (String[] args)
    {
    int LivesLeft;
    String LetterGuessed;
    String wordInput;
    char[] hiddenWord;
    char[] aOfWord;

    Scanner input = new Scanner (System.in);
    boolean isFound;
    int a;

    public Game ()
    {
        this.setLives (10);

        system.out.println (" Player one enter a word:");
        wordInput = input.nextline ();
        aOfWord = wordInput.toCharArray ();
        hiddenWord = new char [aOfWord.length];
        for (int j = 0 ; j < hiddenWord.length ; j++)
            hiddenWord [j] = '*';

        this.output ();

        while (LivesRemaining > 0)
        {
            system.out.println (" Choose a letter: ");
            LetterGuessed = input.nextLine ();

            this.checkForMatch (LetterGuessed);
            if (isFound == true)

                {
                    hiddenWord [a] = LetterGuessed.charAt (0);

                }
            else
            {
                system.out.println(" Not found.");
                this.reduceLives();

             }
             this.output();


          }
   }

public void setLives (int a)
{
    this.LivesRemaining = a;
}

public void reduceLives()
{
    LivesRemaining = LivesRemaining -1;
    system.out.println("Lives left:" + this.getLives());

}

public int getLives()
{
    return LivesRemaining;
}

public void output ()
{
    system.out.println("Lives left" + this.getLives ());
    system.out.println("Progress so far ");

    for (int i = 0; i <hiddenWord.length; i++)
    {
        system.out.print(hiddenWord[i] + "\n");
    }

}

public void checkForMatch(String l)
{

    for(int i=0; i< aOfWord.length; i++)
        { 
            if(l.charAt(0) == aOfWord[i])
            {   
                isFound=true;
                a = i;
                break;
             }
             else
             {  
                isFound = false;
             }
         }
   }
}
}

Upvotes: 0

Views: 2805

Answers (2)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279990

There are tons of compilation errors in your program. First you can't declare methods or constructors in other methods. Your main method seems to contain a Game constructor. That is not syntactically correct. Second, gGet rid of this line

import java.lang.System.out;

It's is not correct because out is a static member. Remember that the java.lang package is always imported implicitly. You could technically do

import static java.lang.System.out;

if you wanted to do

out.println("whatever");

directly instead of

System.out.println("whatever");

but you aren't doing that so the import is unnecessary.

Third, it's System, not system.

Fourth, its Scanner#nextLine(), not Scanner#nextline() as you have here

wordInput = input.nextline();

Finally, there's no instance variable called LivesRemaining declared anywhere.

Upvotes: 1

Keerthivasan
Keerthivasan

Reputation: 12880

You have to use System.out.println everywhere instead of system.out.println. s should be Uppercase.When you are doing import static System.out , you can directly use out.println to print statements in your code. That's the real usage of static import statements in your code. Using static imports should be mostly avoided because it creates confusion to the reader. Try to always use System.out.print in your code wherever needed.

Upvotes: 0

Related Questions