0

Sorry I haven't posted for a few days... I have been very busy moving and had many other issues to deal with... I have changdd the code a bit and added an array to sort and compare a list of player names and scores... I have some compiler errors to sort out which i will post, and also need help finding a way to use the cases in the menu options class to display the sorted lists...

here is the new code:

import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
    class bull2Game{
            public class Player{
            List<Player> players = new ArrayList<Player>(); 
            BufferedReader br=new BufferedReader(new FileReader("C:/Users/Fra..../file1"));
            BufferedReader br2=new BufferedReader(new FileReader("C:/Users/Fra..../file2")); 
            String name = null;
            String score = null; 
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
            teams.add(new player(name, Double.parseDouble(score))); 
            br.close(); 
            br2.close(); 
            Collections.sort(players, new Comparator<player>() { 
                public int compare(player p1, player p2) { 
                    return Double.compare(p2.score, p1.score); 
            for(player p: players) 
            System.out.println(p.name + ' ' + p.score);
            public class scoreBoard { 
                final String name; 
                final double score; 
                public player(String name, double score) { 
                    this.name=name; 
                    this.score=score; 
            }
        }
    }
        public void display_menu() {
            Scanner sc = new Scanner(System.in);
            int choice = getData(sc);
            in.nextInt();     
            System.out.println( " 1) new game new player\n2) new game same player\n3) Sort by Score\n4) Sort by Name\n5) Display Top Score Report\n6) Quit Game " );
            System.out.print( "Selection: " );
        }
    public void menuOptions() {
        Scanner in = new Scanner ( System.in );
        display_menu();
        switch ( in.nextInt()) {
            case 3:
            System.out.println ( "" );
            break;
            case 4:
            System.out.println ( " " );
            break;
            case 5:
            System.out.println ( "" );
            break;
            case 6:
            System.out.println ( "thanks for paying " );
            break;
        }
    }
        public static void main(String args[]){
            int i,j,bull=0,cow=0;int[] secretDigit,arr;
            Scanner scan=new Scanner(System.in);
            String msg, ans, playNextGame;
            secretDigit=new int[4];
            arr=new int[4];
            System.out.println("Bulls and Cows");
            System.out.println("This is an ancient game with guessing numbers.");
            Random rand=new Random();
            Pattern regex=Pattern.compile("^\\d{4}$|^quit$");
            do{
                String secret="";
                playNextGame="";
                for(i=0;i<4;i++){
                    secretDigit[i]=rand.nextInt(10);
                    secret+=secretDigit[i];
                    }
                    System.out.println("A secret number of 4-digit has been draw!");
                    do { 
                        System.out.print("Please enter a 4-digit number (or type 'quit' to exit):");
                        ans=scan.nextLine().trim();
                        System.out.println(ans);
                    }
                    while(!regex.matcher(ans).find());
                    if(ans.equals("quit")){
                        System.out.println("The secret number is: " + secret);
                                }
                                else{
                                    cow=0;
                                    bull=0;
                                    for(i=0;i<4;i++)
                                    try{
                                        arr[i]=Integer.parseInt(ans.substring(i,i+1));
                                        if(arr[i]==secretDigit[i]) bull++;
                                        }
                                        catch(NumberFormatException nfe){}                     
                                        for(i=0;i<4;i++)
                                        for(j=0;j<4;j++)
                                        if(arr[j]==secretDigit[i]){
                                            cow++;
                                            arr[j]=-1;
                                            break;
                                        } 
                                        cow-=bull;
                                        msg="";
                                        if(cow==0 && bull==0){
                                            msg="Neither a bull nor a cow!";
                                                }
                                                else{
                                                    if(bull==4)
                                                    msg="4 bulls! You are a genius!";
                                                    else{
                                                        if(cow==0){
                                                            if(bull==1)
                                                            msg="1 bull only.";
                                                            else
                                                            msg=bull + " bulls only.";
                                                            }
                                                            else if(bull==0){
                                                                if(cow==1)
                                                                msg="1 cow ";
                                                                else
                                                                msg=cow + " cows ";
                                                                }
                                                                else{
                                                                    if(bull==1)
                                                                    msg="1 bull and ";
                                                                    else
                                                                    msg=bull + " bulls and ";
                                                                    if(cow==1)
                                                                    msg+="1 cow.";
                                                                    else
                                                                    msg+=cow + " cows.";
                                                                }
                                                        }
                                                }
                                                System.out.println(msg);
                                    }   
                            }
                            while(bull!=4 && !ans.equals("quit"));
                            if(!ans.equals("quit")){
                                do{
                                    System.out.print("Do you want to play a new game (Y/N)?");
                                    playNextGame=scan.nextLine().trim();
                                    System.out.println(playNextGame);
                            }
                            while(!playNextGame.toUpperCase().equals("Y") && !playNextGame.toUpperCase().equals("N"));
                    } 
                while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
                System.out.println("Thank you for playing Bulls and Cows!");
        }
    }
}
}

and here are the compiler errors:

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.7.0\bin\javac.exe" bull2Game.java
bull2Game.java:11: error: illegal start of type
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
            ^
bull2Game.java:11: error: illegal start of type
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
                   ^
bull2Game.java:11: error: ')' expected
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
                    ^
bull2Game.java:11: error: ';' expected
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
                         ^
bull2Game.java:11: error: <identifier> expected
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
                                       ^
bull2Game.java:11: error: ';' expected
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
                                         ^
bull2Game.java:13: error: <identifier> expected
            br.close(); 
                    ^
bull2Game.java:14: error: <identifier> expected
            br2.close(); 
                     ^
bull2Game.java:15: error: <identifier> expected
            Collections.sort(players, new Comparator<player>() { 
                            ^
bull2Game.java:15: error: <identifier> expected
            Collections.sort(players, new Comparator<player>() { 
                                    ^
bull2Game.java:15: error: illegal start of type
            Collections.sort(players, new Comparator<player>() { 
                                      ^
bull2Game.java:15: error: ')' expected
            Collections.sort(players, new Comparator<player>() { 
                                         ^
bull2Game.java:15: error: ';' expected
            Collections.sort(players, new Comparator<player>() { 
                                                    ^
bull2Game.java:15: error: <identifier> expected
            Collections.sort(players, new Comparator<player>() { 
                                                           ^
bull2Game.java:15: error: illegal start of type
            Collections.sort(players, new Comparator<player>() { 
                                                            ^
bull2Game.java:15: error: <identifier> expected
            Collections.sort(players, new Comparator<player>() { 
                                                             ^
bull2Game.java:15: error: ';' expected
            Collections.sort(players, new Comparator<player>() { 
                                                              ^
bull2Game.java:20: error: illegal start of expression
            public class scoreBoard { 
            ^
bull2Game.java:23: error: invalid method declaration; return type required
                public player(String name, double score) { 
                       ^
bull2Game.java:147: error: class, interface, or enum expected
}
^
20 errors

> Terminated with exit code 1.

as always, any and all help is appreciated, thank you!!

0

The way the code is formatted makes it hard to read. Lines 5 & 6 should not have the same indentation so you can see that line 5 is important relative to line 6:

       public class Player{
            List<Player> players = new ArrayList<Player>(); 
            BufferedReader br=new BufferedReader(new FileReader("C:/Users/Fra..../file1"));
             ...

Why is the Player class the only thing inside of the bull2Game class?
The Player class could be in a file (Player.java) by itself.

The code where the errors happen need to be inside of a method, not just in the class.

0

I fixed the line issue you pointed out:

import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
    class bull2Game{
            public class Player{
                List<Player> players = new ArrayList<Player>(); 
                BufferedReader br=new BufferedReader(new FileReader("C:/Users/Fra..../file1"));
                BufferedReader br2=new BufferedReader(new FileReader("C:/Users/Fra..../file2")); 
                String name = null;
                String score = null; 
                while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
                teams.add(new player(name, Double.parseDouble(score))); 
                br.close(); 
                br2.close(); 
                Collections.sort(players, new Comparator<player>() { 
                    public int compare(player p1, player p2) { 
                        return Double.compare(p2.score, p1.score); 
                        for(player p: players) 
                        System.out.println(p.name + ' ' + p.score);
                        public class scoreBoard { 
                            final String name; 
                            final double score; 
                            public player(String name, double score) { 
                                this.name=name; 
                                this.score=score; 
                        }

and wouldn't putting the player class in its own .java file remove it from the program im writing?

0

You can use many classes in a program. If they are all in the same folder (really on the classpath) the compiler will find their definition.
You can put the definition for more than on class in one .java source file.
Only one can be public:

public class ThePublicClass {
  <  variables and methods go here >
} //  end ThePublicClass

class AnotherClass {
 <  variables and methods go here >
} // end AnotherClass 


class AndAnotherClass {
 <  variables and methods go here >
} // end AndAnotherClass 

Notice none of these classes are inside of another class.

Edited by NormR1

0

ok, I think im almost on the same page here... Now, what would be the best way to declare a method for the array I have in class player?

I have also decided to go a different route with the option menu... and hope that it goes a little better than the previous menu did...

here is the part of the new code with the classes I am focusing on... minus the game ittself because I actually know that if its alone it executes... also for the menu I haven't gotten all the else if loops inserted yet... i'm just seeing if this is going to be a better match than the previous menu I had implemented:

import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
    public class bull2Game{
        class Player{
            List<Player> players = new ArrayList<Player>(); 
            BufferedReader br=new BufferedReader(new FileReader("C:/Users/Fra..../file1"));
            BufferedReader br2=new BufferedReader(new FileReader("C:/Users/Fra..../file2")); 
            String name = null;
            String score = null; 
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null)  
            players.add(new player(name, Double.parseDouble(score))); 
            br.close(); 
            br2.close(); 
            Collections.sort(players, new Comparator<player>() { 
                public int compare(player p1, player p2) { 
                    return Double.compare(p2.score, p1.score); 
                    for(player p: players) 
                    System.out.println(p.name + ' ' + p.score);
                    class scoreBoard { 
                        final String name; 
                        final double score; 
                        public player(String name, double score) { 
                            this.name=name; 
                            this.score=score;
                        }
                        public void menu() { 
                            Scanner scanner = new Scanner(System.in); 
                            while (true) { 
                                System.out.println("\nMenu Options\n"); 
                                System.out.println("(1) New Game -- New Player "); 
                                System.out.println("(2) - New Game -- Same Player ");
                                System.out.println("(2) - Sort By Score ");
                                System.out.println("(2) - Display High Score Report ");
                                System.out.println("(2) - Quit Game "); 
                                System.out.print("Please enter your selection:\t"); 
                                int selection = scanner.nextInt(); 
                                if (selection == 1)
                                playNextGame="";
                                else if (selection == 2) { 

                                }
                            }
                        }
0

There are still problems with the way the code is formatted. The ending }s should be vertically inline below the line with the starting/matching {

Where is the ending } for the method that must end at the return statement on line 17?

Why is line 18 indented the same as line 17?

Why is the constructor for the Player class on line 23 inside the scoreBoard class on line 20?

0

I dont know what you mean by the constructor on line 20... It is supposed to be a separate class... theres the player class and then the scoreboard class which incorporates the player array in the player class with the names and scores in the scoreboard class... at least thats how I intended it... also the tutorial I was looking over on how to do the high score list suggested that it should be done that way... then again... I wouldn't know... im really bad at this stuff...lol

0

I said line 23 not 20 for the constructor.

I suggest that you take the time to correct the formatting of the code. Its a mess now.
Some suggestions:
1)Add a few blank lines and a line of "-"s
//--------------------------------------
before every inner class definiton.
2) Make sure the ending '}' is in line beneath the start of the line with the begining '{'
3) correct the indentation of statements to reflect theie logic nesting level. For example line 39 should be indented.

The reason for having correct formatting for the code is so it can be read and understood easily. Unformatted code is very hard to understand.

0

I think I got the formatting fixed... and I moved the constructor from line 23 I hope that is what you were talking about...

import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
    public class bull2Game{
        //--------------------------------------------------
        class Player{
            List<Player> players = new ArrayList<Player>(); 
            BufferedReader br=new BufferedReader(new FileReader("C:/Users/Fra..../file1"));
            BufferedReader br2=new BufferedReader(new FileReader("C:/Users/Fra..../file2")); 
            String name = null;
            String score = null; 
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null);  
            players.add(new player(name, Double.parseDouble(score))); 
            br.close(); 
            br2.close(); 
            Collections.sort(players, new Comparator<player>() { 
                public int compare(player p1, player p2) { 
                    return Double.compare(p2.score, p1.score); 
                }
                public player(String name, double score) { 
                            this.name=name; 
                            this.score=score;
                }
                for(player p: players) 
                System.out.println(p.name + ' ' + p.score);
                //------------------------------
                class scoreBoard { final String name; 
                final double score; 
                }
                //--------------------------------
                public void menu() { 
                    Scanner scanner = new Scanner(System.in); 
                    while (true) { 
                        System.out.println("\nMenu Options\n"); 
                        System.out.println("(1) New Game -- New Player "); 
                        System.out.println("(2) - New Game -- Same Player ");
                        System.out.println("(3) - Sort By Score ");
                        System.out.println("(4) - Display High Score Report ");
                        System.out.println("(5) - Quit Game "); 
                        System.out.print("Please enter your selection:\t"); 
                        int selection = scanner.nextInt(); 
                        if (selection == 1){
                            playNextGame="";
                        }
                        else if (selection == 2) { 
                        }
                    }
                }
                    //----------------------------------------------------
0

the compiler errors are as follows:

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.7.0\bin\javac.exe" bullGameMenu.java
bullGameMenu.java:12: error: illegal start of type
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null);  
            ^
bullGameMenu.java:12: error: illegal start of type
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null);  
                   ^
bullGameMenu.java:12: error: ')' expected
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null);  
                    ^
bullGameMenu.java:12: error: ';' expected
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null);  
                         ^
bullGameMenu.java:12: error: <identifier> expected
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null);  
                                       ^
bullGameMenu.java:12: error: ';' expected
            while ((score = br.readLine()) != null && (name = br2.readLine()) != null);  
                                         ^
bullGameMenu.java:13: error: <identifier> expected
            players.add(new player(name, Double.parseDouble(score))); 
                       ^
bullGameMenu.java:13: error: illegal start of type
            players.add(new player(name, Double.parseDouble(score))); 
                        ^
bullGameMenu.java:13: error: ')' expected
            players.add(new player(name, Double.parseDouble(score))); 
                           ^
bullGameMenu.java:13: error: ';' expected
            players.add(new player(name, Double.parseDouble(score))); 
                                  ^
bullGameMenu.java:13: error: <identifier> expected
            players.add(new player(name, Double.parseDouble(score))); 
                                       ^
bullGameMenu.java:13: error: ';' expected
            players.add(new player(name, Double.parseDouble(score))); 
                                               ^
bullGameMenu.java:13: error: invalid method declaration; return type required
            players.add(new player(name, Double.parseDouble(score))); 
                                                ^
bullGameMenu.java:13: error: <identifier> expected
            players.add(new player(name, Double.parseDouble(score))); 
                                                                 ^
bullGameMenu.java:13: error: ';' expected
            players.add(new player(name, Double.parseDouble(score))); 
                                                                  ^
bullGameMenu.java:14: error: <identifier> expected
            br.close(); 
                    ^
bullGameMenu.java:15: error: <identifier> expected
            br2.close(); 
                     ^
bullGameMenu.java:16: error: <identifier> expected
            Collections.sort(players, new Comparator<player>() { 
                            ^
bullGameMenu.java:16: error: <identifier> expected
            Collections.sort(players, new Comparator<player>() { 
                                    ^
bullGameMenu.java:16: error: illegal start of type
            Collections.sort(players, new Comparator<player>() { 
                                      ^
bullGameMenu.java:16: error: ')' expected
            Collections.sort(players, new Comparator<player>() { 
                                         ^
bullGameMenu.java:16: error: ';' expected
            Collections.sort(players, new Comparator<player>() { 
                                                    ^
bullGameMenu.java:16: error: <identifier> expected
            Collections.sort(players, new Comparator<player>() { 
                                                           ^
bullGameMenu.java:16: error: illegal start of type
            Collections.sort(players, new Comparator<player>() { 
                                                            ^
bullGameMenu.java:16: error: <identifier> expected
            Collections.sort(players, new Comparator<player>() { 
                                                             ^
bullGameMenu.java:16: error: ';' expected
            Collections.sort(players, new Comparator<player>() { 
                                                              ^
bullGameMenu.java:20: error: invalid method declaration; return type required
                public player(String name, double score) { 
                       ^
bullGameMenu.java:24: error: illegal start of type
                for(player p: players) 
                ^
bullGameMenu.java:24: error: ')' expected
                for(player p: players) 
                            ^
bullGameMenu.java:24: error: <identifier> expected
                for(player p: players) 
                                     ^
bullGameMenu.java:25: error: <identifier> expected
                System.out.println(p.name + ' ' + p.score);
                                  ^
bullGameMenu.java:25: error: <identifier> expected
                System.out.println(p.name + ' ' + p.score);
                                         ^
bullGameMenu.java:25: error: ';' expected
                System.out.println(p.name + ' ' + p.score);
                                           ^
bullGameMenu.java:25: error: illegal start of type
                System.out.println(p.name + ' ' + p.score);
                                                ^
bullGameMenu.java:25: error: ';' expected
                System.out.println(p.name + ' ' + p.score);
                                                   ^
bullGameMenu.java:25: error: <identifier> expected
                System.out.println(p.name + ' ' + p.score);
                                                         ^
36 errors

> Terminated with exit code 1.
0

That looks like the same error you got before: * code outside of method *
that was discussed in an earlier post.

0

I'm not sure what kind of method to call for the player class... a return? an instance?

0

The code where the errors needs to be** INSIDE** of a method. It has nothing to do with calling a method.
Look at the code at the error, see what is does, make a suitable name for the method and wrap the code where the errors are in a method definition

0

Please put your code in try and catch have exception IOException because scanner class throws IOException!

0

Please be precise when posting advice; many of your readers will be beginners. A class cannot throw an Execption. Only a method or constructor can do that.
There is no Scanner method that throws an IOE. Two of the constructors for Scanner can thow an IOE, but not the one that's used in this code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.