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!!