Java - How To Compare Elements In An Object List - java

I am trying to get and compare average scores of a Player object stored in a List, where every object has a first name, last name, and a list of scores. An example of this would be (List with one object):
[0] Firstname: "Michael", Lastname: "Morris", Scores: [88, 92]
I have code with a nested for loop within a for loop that gives me an OutOfBounds error. I need to get the average of scores per Player and return the highest Player with the highest average. Here's what I have at the moment (The naming is kind off):
public static PlayerScores findHighestScorer(List<PlayerScores> players) {
String team = players.toString();
PlayerScores thePlayers = new PlayerScores("", ""); // What to return
for (int i = 0; i < team.length() - 1; ++i) { // Get every player and their average score
PlayerScores player = players.get(i); // First player
int avg = player.getAverage();
for (int j = i + 1; j < team.length(); ++j) {
PlayerScores player2 = players.get(j); // FIXME: OutOfBoundsException Error
int avg2 = player2.getAverage();
if (avg2 < avg) {
thePlayers = player;
} else if (avg2 > avg) {
thePlayers = player2;
} else {
thePlayers = player;
}
}
}
return thePlayers;
Any and all suggestions would be very helpful, thank you!

I hope this solves your problem! Cheers.
public static PlayerScores findHighestScorer(List<PlayerScores> players) {
PlayerScores bestScore = null;
// loop through list...
for (PlayerScores score : players) {
if (bestScore == null) {
bestScore = score;
} else {
if (bestScore.getAverage() < score.getAverage()) {
bestScore = score;
}
}
}
return bestScore;
}

Related

Persistent java.lang.NullPointerException error from String.isEmpty()

I've been trying to search for the bug, but I couldn't find it. Already spent an hour trying to resolve what's wrong. The error begins when the code enters the isPlayerSet method while (!player.isPlayerSet()) {. I already set the used properties to "" but I am still getting this nullpointerexeption error. Please understand that I am fairly new in programming, especially in Java.
Here's the main class
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String playerName = "";
int chosenPokemon = 0;
boolean isANumber = false;;
Player player;
/*
* Initialize Players
*/
Player[] players = new Player[2];
for (int counter = 0; counter < players.length; counter++) {
player = new Player();
}
/*
* Get details of trainers
*/
for (int counter = 0; counter <= players.length-1; counter++) {
player = players[counter];
while (!player.isPlayerSet()) {
/*
* Input player name
*/
if(player.getPlayerName() == "") {
System.out.println("Enter a valid name for Player " + (counter+1) + ":");
player.setPlayerName(playerName);
}
/*
* Choose Pokemon
*/
if(player.getChosenPokemon() == ""){
System.out.println("Choose a starting pokemon for Player " + (counter+1) + ":");
System.out.println("[1] Charmander");
System.out.println("[2] Bulbasaur");
System.out.println("[3] Squirtle");
do {
if(!scanner.hasNextInt())
{
System.out.println("Input must be a valid integer. Try Again.");
scanner.next();
}
else if(!(chosenPokemon >= 1) && !(chosenPokemon <= 3))
{
System.out.println("Input must be a number from 1-3. Try Again.");
scanner.next();
}
else {
chosenPokemon = scanner.nextInt();
isANumber = true;
}
} while(!isANumber);
player.setChosenPokemon(chosenPokemon);
}
} // End of while loop
} // End of for loop
}
}
And here's the player class
public class Player {
Scanner scanner = new Scanner(System.in);
private String playerName = "";
private String chosenPokemon = "";
public String getPlayerName() {
return this.playerName;
}
public void setPlayerName(String playerName) {
do {
playerName = scanner.nextLine();
if(!isAlpha(playerName)) {
System.out.println("Invalid input. Try again");
}
if(playerName.isEmpty()) {
System.out.println("Player name cannot be blank! Try again");
}
} while(!isAlpha(playerName) || playerName.isEmpty());
this.playerName = playerName;
System.out.println("Welcome " + this.playerName);
}
public String getChosenPokemon() {
return chosenPokemon;
}
public void setChosenPokemon(int chosenPokemon) {
if(chosenPokemon == 1) {
this.chosenPokemon = "Charmander";
} else if(chosenPokemon == 2) {
this.chosenPokemon = "Bulbasaur";
} else {
this.chosenPokemon = "Squirtle";
}
}
public boolean isPlayerSet() {
if (this.playerName.isEmpty() && this.chosenPokemon.isEmpty()) {
return false;
}
return true;
}
public static boolean isAlpha(String name) {
char[] chars = name.toCharArray();
for (char c : chars) {
if (!Character.isLetter(c)) {
return false;
}
}
return true;
}
}
I also have another question, is it advisable to replace players[counter] with Player player?
You are creating new Player objects here:
for (int counter = 0; counter < players.length; counter++) {
player = new Player();
}
But: you are not storing those players in the array that you defined above. Thus: the array elements stay at its initial value - meaning that all players in the player array ... are null.
So your loop should say
players[counter] = new Player();
And of course, you really want to read this here.
In the loop
for (int counter = 0; counter < players.length; counter++) {
player = new Player();
}
You initialize the local variable player, so in while (!player.isPlayerSet()) player is null. You need to initialize the instance in the players array
for (int counter = 0; counter < players.length; counter++) {
players[counter] = new Player();
}
You're clobbering the same variable within this loop.
for (int counter = 0; counter < players.length; counter++) {
player = new Player();
}
Option 1:
for (int counter = 0; counter < players.length; counter++) {
players[counter] = new Player();
}
Option 2 (Slightly more concise and elegant):
for (Player p: players) p = new Player();
This
for (int counter = 0; counter < players.length; counter++) {
player = new Player();
}
should be this.
for (int counter = 0; counter < players.length; counter++) {
players[counter] = new Player();
}

How to count the occurrences of random instances

For example I have a programming assignment that wants me to make a coin class and a driver class that randomly flips a coin 40 times and then counts at the very end how many times it ends up heads and tails. Well I got the entire code so far it being:
public class driver {
public static void main(String[] args) {
coin myCoin = new coin();
System.out.println("Coin initially is " + myCoin.getSideUp());
for (int i = 0; i < 40; i++) {
myCoin.toss();
System.out.println("Coin is now " + myCoin.getSideUp());
}
}
}
public class coin {
protected String sideUp;
public coin() {
if (Math.random() < 0.5)
this.sideUp = "heads";
else
this.sideUp = "tails";
}
public void toss() {
if (Math.random() < 0.5)
this.sideUp = "heads";
else
this.sideUp = "tails";
}
public String getSideUp() {
return this.sideUp;
}
}
That's all done, but how do I count the instances of each heads or tails?
Count them as the tosses are made instead of at the end. If the assignment prohibits this, save the results to an array and count the results from the array at the end.
Just check and see if your coin is head or tail after each toss.
You could keep the counter for each status(head/tail) in an array like below:
Coin myCoin = new Coin();
System.out.println("Coin initially is " + myCoin.getSideUp());
int[] coinCount = new int[2];
for (int i = 0; i < 40; i++) {
myCoin.toss();
System.out.println("Coin is now " + myCoin.getSideUp());
if(myCoin.getSideUp().equals("heads")){
coinCount[0]++;
} else {
coinCount[1]++;
}
}
System.out.println("Heads: "+coinCount[0]);
System.out.println("Tails: "+coinCount[1]);

Java finding place in an array?

So I have to find the minimum in an array in Java, but with that I have to print out the corresponding names that go with the minimum in another parallel array. Inside my for loop where I find the minimum, I have a variable place that I set equal to my counter variable from the for loop when the minimum is changed. But every time I print out the name, it prints out the first name in the array instead of the name in the place holder.
public double getMinimum(double[] money)
{
double lowest = money[0];
for (int p = 0; p < money.length; p++)
{
if (money[p] < lowest)
{
lowest = money[p];
place = p;
}
}
return lowest;
}
Theres the for loop within my programmer-defined class that finds the minimum.
public String getFirstNameMin(String[] firstName)
{
String minFirstName;
minFirstName = firstName[place];
return minFirstName;
}
This is the code I'm using to figure out the first name from the first names array at that place. What am I doing wrong? I'm kinda new to Java, but I did all this array stuff in C++ before, so idk if I am missing something very simple, or its different in Java.
I would say try making a separate class for this that contains the user and the money:
public class User {
private double money;
private String fname;
private String lname;
//getters/setters/constructors
}
Then from there you can simply compare the accounts:
public User getMinimum(User[] users) {
if (users.length <= 0) {
return null;
}
User lowest = users[0];
for (int i = 1; i < users.length; i++) {
if (users[i].getMoney() < lowest.getMoney()) {
lowest = users[i];
}
}
return lowest;
}
Try this:
public int getMinIndex(double[] money)
{
double min = money[0];
int minIndex = 0;
for (int p = 0; p < money.length; p++)
{
if (money[p] < min)
{
min = money[p];
minIndex = p;
}
}
return minIndex;
}
public static void main(String[] args)
{
double[] money;
String[] name;
//... init your arrays here!
int index = getMinIndex(money);
System.out.println("Min money = " + money[index] + "; name = " + name[index]);
}
However, following an object oriented approach rogues solution is much nicer!!!

Identify an item from array?

I have 3 arrays, working parallel. I need to give the user the ability to identify an item from the array, and remove it and its information, and or edit its information.
This is what I have so far:
private static int identifyComputer(String[] computerBrand, double[] computerSpeed, double[] computerPrice) {
Scanner keyboard = new Scanner(System.in);
int counter = computerBrand.length;
System.out.println("Computer brand?");
String cb = keyboard.nextLine();
int i = 0;
boolean notFound = true;
for (i = 0; i < counter && notFound; i++)
{
if (cb.equals(computerBrand[i]))
{
System.out.println(computerBrand[i]);
System.out.println(computerSpeed[i]);
System.out.println(computerPrice[i]);
notFound = false;
}
if (notFound) {
return -1;
}
else
{
System.out.println("Computer Speed?");
String cs = keyboard.nextLine();
boolean notFound2 = true;
for (i = 0; i < counter && notFound2; i++)
{
if (cs.equals(computerSpeed[i]))
{
System.out.println(computerBrand[i]);
System.out.println(computerSpeed[i]);
System.out.println(computerPrice[i]);
notFound2 = false;
}
}
if (notFound) {
return -1;
} else {
System.out.println("Computer Price?");
String cp = keyboard.nextLine();
boolean notFound3 = true;
for (i = 0; i < counter && notFound3; i++)
{
if (cp.equals(computerPrice[i]))
{
System.out.println(computerBrand[i]);
System.out.println(computerSpeed[i]);
System.out.println(computerPrice[i]);
notFound3 = false;
}
}
if (notFound) {
return -1;
}
}
}
}
return i;
}
But I know that isnt how you do it 100%. It has some errors on the for loops and it doesnt work correctly. I was trying to get the user to be able to indentify the computer and return the index of that computer. But I am also unsure of that too.
(I CANNOT USE ARRAYLISTS)
It's confusing to store the information in three arrays in parallel. I strongly recommend you create a new type of object with all the information, and create a single array of that object:
class Computer {
String brand;
int speed;
double price;
}
Next, create an array of Computer objects and manipulate the variables in class Computer through that array. Your code will be much easier to write.

How to print the maximum valued path in a 2D array in Java?

I guess you all know the "strawberry" problem that some give you in job interviews, where you need to calculate the path between 2 corners of a 2D array that you can only move up or to the right and you have the calculate the maximum valued path.
I have a perfectly working code that does it in Recursion, but it's complexity is to high.
i also solved the problem in the "for loop" solution that does it in O(n^2) complexity.
but in this solution i just couldn't figure out a way to print the route like i did in the recursion solution.
This is my code (it is quite long to read here so i guess you should copy,compile and run).
look at the results of the recursion solution, BTW - The path needs to be from the left bottom corner to the right upper corner
I want to print the route the same way in the better solution:
public class Alg
{
public static void main(String args[])
{
String[] route = new String[100];
int[][]array = {{4,-2,3,6}
,{9,10,-4,1}
,{-1,2,1,4}
,{0,3,7,-3}};
String[][] route2 = new String[array.length][array[0].length];
int max = recursionAlg(array,array.length-1,0,route);
int max2 = loopAlg(array,array.length-1,0,route2);
System.out.println("The max food in the recursion solution is: "+max);
System.out.println("and the route is: ");
printRouteArray(route);
System.out.println("The max food in the loop solution: "+max2);
System.out.println("The route is: ");
//SHOULD PRINT HERE THE ROUTE
}
public static int loopAlg(int [][] arr,int x, int y, String[][] route)
{
int n=0;
int[][]count = new int[arr.length][arr[0].length];
for(int i = x; i>=0 ; i--)
{
for(int j = 0; j<arr[0].length; j++)
{
if (i==x && j==0) {count[i][j]=arr[i][j];}
else if (i == x) { count[i][j]=count[i][j-1]+arr[i][j];}
else if (j == 0) { count[i][j]=count[i+1][j]+arr[i][j]; }
else{
if (count[i][j-1]>count[i+1][j]) {count[i][j]=count[i][j-1]+arr[i][j];}
else { count[i][j]= count[i+1][j]+arr[i][j];}
}
}
}
return count[0][arr[0].length-1];
}
public static int recursionAlg(int [][] arr, int x, int y,String[] route)
{
return recursionAlg(arr,0,x,y,arr[0].length-1,route,0);
}
public static int recursionAlg(int[][]arr,int count,int x, int y, int max_y, String[] route, int i)
{
if (x == 0 && y == max_y) {return count;}
else if (x == 0) {
route[i]="Right";
return recursionAlg(arr,count+arr[x][y+1],x,y+1,max_y,route,i+1);
}
else if (y==max_y){
route[i]="Up";
return recursionAlg(arr,count+arr[x-1][y],x-1,y,max_y,route,i+1);
}
else if (recursionAlg(arr,count+arr[x-1][y],x-1,y,max_y,route,i+1)>recursionAlg(arr,count+arr[x][y+1],x,y+1,max_y,route,i+1))
{
route[i]="Up";
return recursionAlg(arr,count+arr[x-1][y],x-1,y,max_y,route,i+1);
}
else
{
route[i]="Right";
return recursionAlg(arr,count+arr[x][y+1],x,y+1,max_y,route,i+1);
}
}
public static void printRouteArray(String[] arr)
{
int i=0;
while (i<arr.length && (arr[i]=="Up" || arr[i]=="Right"))
{
System.out.print(arr[i]+"-->");
i++;
}
System.out.println("End");
}
}
Hope you can help, thanks!
You need another 2-dimensional array inside loopAlg that memorizes which step to take to come to this next entry for every entry in your initial 2-dim array. See the following code and https://ideone.com/kM8BAZ for a demo:
public static void main(String args[])
{
String[] route = new String[100];
int[][]array = {{4,-2,3,6}
,{9,10,-4,1}
,{-1,2,1,4}
,{0,3,7,-3}};
String[] route2 = new String[100];
int max = recursionAlg(array,array.length-1,0,route);
int max2 = loopAlg(array,array.length-1,0,route2);
System.out.println("The max food in the recursion solution is: "+max);
System.out.println("and the route is: ");
printRouteArray(route);
System.out.println("The max food in the loop solution: "+max2);
System.out.println("The route is: ");
printRouteArray(route2);
}
public enum Dirs {START, FROM_LEFT, FROM_DOWN};
public static int loopAlg(int [][] arr,int x, int y, String[] route)
{
int n=0;
int[][]count = new int[arr.length][arr[0].length];
Dirs[][] directions = new Dirs[arr.length][arr[0].length];
List<String> path = new ArrayList<String>();
for(int i = x; i>=0 ; i--)
{
for(int j = 0; j<arr[0].length; j++)
{
if (i==x && j==0) {count[i][j]=arr[i][j]; directions[i][j] = Dirs.START;}
else if (i == x) { count[i][j]=count[i][j-1]+arr[i][j]; directions[i][j] = Dirs.FROM_LEFT;}
else if (j == 0) { count[i][j]=count[i+1][j]+arr[i][j]; directions[i][j] = Dirs.FROM_DOWN;}
else{
if (count[i][j-1]>count[i+1][j]) {count[i][j]=count[i][j-1]+arr[i][j];directions[i][j] = Dirs.FROM_LEFT;}
else { count[i][j]= count[i+1][j]+arr[i][j];directions[i][j] = Dirs.FROM_DOWN;}
}
}
}
int i=0, j=arr[0].length-1;
while(directions[i][j]!= Dirs.START) {
if(directions[i][j] == Dirs.FROM_LEFT) {
path.add("Right");
j--;
}
else {
path.add("Up");
i++;
}
}
Collections.reverse(path);
i=0;
for(String part:path) {
route[i] = part;
i++;
}
return count[0][arr[0].length-1];
}

Categories

Resources