Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: please help me to fix my code ,this code for bulls and cows game ,(i linked the errors in the massege)

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default please help me to fix my code ,this code for bulls and cows game ,(i linked the errors in the massege)

    void play()
    {

    int j=0;
    Result test=new Result();
    int [] arr = new int[4];
    Game player1=new Game();
    Game player2=new Game();
    Random rand = new Random();
    int n = rand.nextInt(5040) + 1;
    Scanner input=new Scanner(System.in);

    j++;
    Choice come=new Choice(array1[n].arr);
    while(j>0)
    {
    System.out.printf("enter your guesss:");



    [COLOR="#FF0000"]
    for(int i=0 ;i<4;i++)
    {

    arr[i]=input.nextInt();


    }[/red]





    come.testChoice(arr, test);
    if(test.Bulls==4)
    {
    System.out.printf("you guesses the number you have win\n");
    break;
    }

    System.out.printf("your scored %d balls and %d cows",test.Bulls,test.kiliot);
    player1.filter(arr,test.Bulls,test.kiliot);
    if(player2.makeAChoice()==1)
    {
    System.out.printf("computer win tha game\n");
    break;
    }
    j++;
    }
    input.close();


    }

    }



    i run mu code and show me these errors :
    enter your guesss:1 2 3 4
    your scored 0 balls and 1 cows
    My guess is :
    2 6 7 8
    Enter Bulls:

    1
    Enter Kliots:
    2
    enter your guesss: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 Game.play(Game.java:130)
    at Game.main(Game.java:51)


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: please help me to fix my code ,this code for bulls and cows game ,and i run it and their show me errors???

    What is your question?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help me to fix my code ,this code for bulls and cows game ,(i linked the errors in the massege)

    i upteded it

  4. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: please help me to fix my code ,this code for bulls and cows game ,(i linked the errors in the massege)

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    enter your guesss: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 Game.play(Game.java:130)
    At line 130 the code called the nextInt() method when there was not anything to read.
    Make sure there is data to be read before trying to read it. For example the hasNextInt() will say if there is a number ready to be read. Or change the code or input file so the reads that are done matches the data in the file.

  5. #5
    Junior Member
    Join Date
    Nov 2015
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help me to fix my code ,this code for bulls and cows game ,(i linked the errors in the massege)

    i got a project of bulls cows game :exemple:secret number(generated by computer)=1234
    user number =2584(bulls=1 because 4=4 same line
    cows=1 because we got 2 but not same line)
    we can t repeat a digit for the secret number or the user number.


    i writed my code but can t solve this problem keep getting crashes or stays in the loop
    the problem if u try the code and use like 123 it stays in the loop so if user number<4 ||number>4(4 digits)

    String numbre;
    boolean finished=false;
    do{int bulls=0;
    int cows=0;
    int guess=0;
    int k;
    Scanner s=new Scanner(System.in);
    int secret=(int)(Math.random()*9000+1000);
    while(secretnumber(secret)==true)
    {secret=(int)(Math.random()*9000+1000);}
    System.out.println("the secret number here "+secret);
    System.out.println("enter 4 digit");
    numbre=s.next();
    k=Integer.parseInt(numbre);
    String k1=k+"";
    while(secretnumber(k)==true||k1.length()!=4)
    {System.out.println("enter 4 digit");
    k=s.nextInt();}
    while(numbre.length()!=4)
    {System.out.println("enter 4 digit");
    numbre=s.next();}
    String hiding=secret+"";
    for(int i=0;i<4;i++)
    {if(hiding.charAt(i)==numbre.charAt(i))
    {bulls++;}
    else
    {if(hiding.contains(numbre.charAt(i)+""))
    {cows++;}}}
    if(bulls==4)
    {finished=true;
    System.out.println("bulls "+bulls+" cows "+cows);}
    else
    {System.out.println("bulls "+bulls+" cows "+cows);}}while(finished!=true);
    System.out.println("thx for playing");}
    public static boolean secretnumber(int num){
    boolean[] table = new boolean[10];//boolean it always false
    while(num > 0){
    if(table[num%10])
    return true;
    table[num%10] = true;
    num/= 10;}
    return false;}}

Similar Threads

  1. Cow and Bulls Game Help (Urgent)
    By Cyclone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 27th, 2013, 10:55
  2. Getting Exception Errors, But Not Sure How To Fix Code
    By noel222 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2012, 11:09
  3. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 20:30
  4. How to fix this code?
    By ice in forum AWT / Java Swing
    Replies: 26
    Last Post: November 29th, 2010, 18:10