-1

This is my Main Method

package game1;

public class game1 {

    public static int c;

    public static void main(String args[])
    {
        System.out.print("Welcome to my Game of Nim\n\n");
        toss TOSS = new toss();
        toothpicks TOOTHPICKS = new toothpicks();
        letsPlay START = new letsPlay();
        c = TOSS.WhoGoesFirst();
        toothpicks.number = TOOTHPICKS.HowMany();

        while(toothpicks.number > 1)
        {
            if(c==0 || c==2)
            {
                toothpicks.number = START.yourTurn();
            }   
            else if(c==1 || c==2)
            {
                toothpicks.number = START.myTurn();
            }
            c=2;
        }

    }

}

And this is a class which i built to play the game

package game1;
import java.util.Scanner;

public class letsPlay {

    Scanner input = new Scanner(System.in);
    int remove;
    public int yourTurn()
    {
         if(toothpicks.number == 1)
        {
            System.out.print("YOU LOSE");
            return 0;
        }

        System.out.print("Your turn. There are " +toothpicks.number+"       toothPicks left. ");
        System.out.print("How many You want to remove?");
        remove = input.nextInt();
        toothpicks.number -= remove;

        return toothpicks.number;
    }

    public int myTurn()
     {
        if(toothpicks.number == 1)
        {
            System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. \n");
            System.out.print("I,the Computer, will remove it\n");
            System.out.print("YOU WIN");
            remove=1;
            toothpicks.number -= remove;
        }
        else if(toothpicks.number==2)
        {
            System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
            remove = 1;
            System.out.print("I,the Computer, will remove "+ remove +" toothPicks\n");
            toothpicks.number -= remove;
        }
        else if(toothpicks.number==3)
        {
            System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
            remove = 2;
            System.out.print("I,the Computer, will remove "+remove +" toothPicks\n");
            toothpicks.number -= remove;
        }
        else
        {
            System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
            remove =1 + (int)(Math.random()*4); 
            System.out.print("I,the Computer, will remove "+remove +" toothPicks.\n");
            toothpicks.number-=remove;
        }

         input.close();
         return toothpicks.number;
      }

 }

EDIT :This is toothpicks Class

   public class toothpicks {

   public static int number;
   public int HowMany()
   {
        Scanner input =new Scanner(System.in);
        do
        {
            if(game1.c==1)
            {
                System.out.print("I choose the no. of toothPicks to be " );
                number = input.nextInt(); 
                if(number<20 || number>30)
                {
                    System.out.print("ToothPicks Should be between 20-30\n");
                }
            }
            else
            {
                number = 20 + (int)(Math.random()*10);
                System.out.print("computer has choosed "+ number +" Toothpicks \n");
            }

        }while(number<20 || number>30);

        input.close();
        return number;

    }

    }

I also made other class toss for toss. When I run this program it cannot work something going wrong in letsPlay class which i cannot figure out yet as I'm very new to JAVA, Program stops before it will ask me to input how many toothpicks i want to remove. Is anything is going wrong with the scanner input or what is it?Any help would be appreciated.

OUTPUT Welcome to my Game of Nim

 This time Computer are going to go first
 I choose the no. of toothPicks to be 23
My turn. There are 23 toothPicks left. I,the Computer, will remove 4  toothPicks.
Your turn. There are 19 toothPicks left. How many You want to remove?   Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at game1.letsPlay.yourTurn(letsPlay.java:18)

at game1.game1.main(game1.java:21)

Please Note number is a static variable which I declare in toothpicks class.

  • You say "something going wrong in letsPlay class", but what exactly - what input do you provide? What have you tried to resolve this issue? – Edd Aug 23 '15 at 13:29
  • I Don't exactly know as i figured out it, because program stops just before i am going to give it a input everything before it works fine and according to program requirement i provide a valid input which is integer. – Prince Vijay Pratap Aug 23 '15 at 13:33
  • 2
    As an aside, it's convention in Java for _types_ to start with `CapitalLetters`, _variables_ and _fields_ to be in `camelCaseWithLowerCaseFirstLetters`, and _constants_ to be in `BLOCK_CAPITALS`. – Edd Aug 23 '15 at 13:34
  • Since the only way for the code to never enter the loop (and thus not ask for any input) is if `toothpicks.number` is less than or equal to `1`, we'll need to see what value it's initialised to (i.e. what `toothpicks.HowMany()` does) – Edd Aug 23 '15 at 13:41
  • @Edd I EDITED it please check out! – Prince Vijay Pratap Aug 23 '15 at 13:49
  • It is always a great idea to post the exception you're getting. Then you should read this: [java.util.NoSuchElementException - Scanner reading user input](http://stackoverflow.com/a/13042296). It might not be the exact same exception, but the source/reason is the same. – Tom Aug 23 '15 at 14:01
  • @PrinceVijayPratap Based on what you've posted so far I can't see any reason why the program would just exit without an error, printing any output or asking for user input as I understand your claiming. If there is any output or exceptions we'll need to see them. – Edd Aug 23 '15 at 14:06
  • One problem I immediately see is that you're using more than one Scanner that is built on the `System.in`, and then closing a Scanner, which is dangerous to do since it can lead to unexpected behavior should the other Scanner still be using the `System.in`. I suggest that you refactor this code, and extract all user input and output out of **all** classes except for a one single UI class, and that you use a single Scanner that uses `System.in`. – Hovercraft Full Of Eels Aug 23 '15 at 14:10
  • @Edd I posted the output which i'm getting, take a look – Prince Vijay Pratap Aug 23 '15 at 14:53
  • Might be a good idea to take the two minute to read the link that was posted in one of the comments .... – Tom Aug 23 '15 at 15:09
  • @Tom I removed the `input.close()` from the `toothpicks class` and now it works fine can u tell me why?Is it because that i'm still using `number` variable after closing it. – Prince Vijay Pratap Aug 23 '15 at 15:21
  • possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](http://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – Edd Aug 23 '15 at 17:10

1 Answers1

-1

Try this -

First, don't forget to import java.util.Scanner in your classes that use it.

When requesting integers from java.util.Scanner, you must do the following:

Scanner s = new Scanner(System.in);
int i = s.nextInt();
s.nextLine();

Try that. Put s.nextLine() right after any s.nextInt() uses.

Also, don't run s.close() until your program is completely finished. Otherwise, it closes the InputStream from System.in, which is mostly always unwanted.

ExcelsiorVFX
  • 31
  • 1
  • 7
  • No, it doesn't do anything and i receive the same error. – Prince Vijay Pratap Aug 23 '15 at 15:04
  • That `NoSuchElementException` would usually be fixed by my solution above. I'm not sure why that doesn't work. – ExcelsiorVFX Aug 23 '15 at 16:12
  • i cant undestand why u want me to use nextLine(); after next.Int(); – Prince Vijay Pratap Aug 23 '15 at 16:18
  • Scanner interprets a "Line" as a string, like this: "Hello\n", with "\n" at the end. "\n" is equivalent to hitting the enter key on your keyboard. When you get an int, Scanner drops the "\n", so when you do `nextLine()`, it returns "\n", no matter what you typed in. Thus, adding `nextLine()` allows you to use the Scanner again. – ExcelsiorVFX Aug 23 '15 at 16:21