Sports schedule not showing string [duplicate] - java

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 4 years ago.
It's a program that is a sports league, a group of teams plays through a Schedule of Games and determine the winner.
The program runs fine to the point where the final output won't let me see the winner, it shows this instead:
The season winner(s) with 9 points: [Ljava.lang.String;#e73f9ac
I changed it to teams.length which made the program work but it would show me the teams (i) number instead of the string name like "Vancouver".
Thanks in advance.
}
int peak = 0;
int[] total = new int[teams.length];
for (int i=0; i<teams.length; i++){
total[i] = 2*wins[i]+ties[i];
if (total[i] > peak) peak = total[i];
System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + " losses, " + ties[i] + " ties = " + total[i]);
}
System.out.println("The season winner(s) with " + peak + " points: " + teams);
for (int i=0; i<teams.length; i++){
if (peak < total[i]) peak = total[i];
}
}
static int indexOfTeam(String team, String[] teams){
for (int i=0; i<teams.length; i++)
if (team.compareTo(teams[i]) == 0) return i;
return -1;
}
}

Instead of printing the winning team you're printing the teams array.
While iterating store besides the peak, the index of the winning team:
int index = -1;
for (int i=0; i<teams.length; i++){
total[i] = 2*wins[i]+ties[i];
if (total[i] > peak) {
index = i;
peak = total[i];
}
System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + "
losses, " + ties[i] + " ties = " + total[i]);
}
and finally:
System.out.println("The season winner(s) with " + peak + " points: " +
teams[index]);

teams is an array of Strings, you must insert the index after the name

If you want to print all teams use Arrays.toString(teams), but I think that you would like to print only part of teams array, so you can create list of winners
List<String> winners = new ArrayList<Integer>;
for (int i=0; i<teams.length; i++){
total[i] = 2*wins[i]+ties[i];
if (total[i] > peak) {
winners.add(teams[i]);
peak = total[i];
}
System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + "
losses, " + ties[i] + " ties = " + total[i]);
}
indexes object you can simply print
System.out.println("The season winner(s) with " + peak + " points: " +winners);
If you want to have an array you can use toArray() on winners object.

Related

How to let a for loop do my objectList printing

Hi I want to put my prints in a for-loop. how to do it? So something like
if index = 0,1,2 print.
if index = 2,3,4 print.
if index = 4,5,6 print.
Code:
ArrayList<Object> objectList = new ArrayList<Object>(res);
System.out.println("\n\nThis starts to look like calculations:");
System.out.print("\n" + objectList.get(0));
System.out.print(" "+ objectList.get(1));
System.out.print(" " + objectList.get(2) + " =");
System.out.print("\n\n" + objectList.get(2));
System.out.print(" " + objectList.get(3));
System.out.print(" " + objectList.get(4)+ " =");
System.out.print("\n\n" + objectList.get(4));
System.out.print(" " + objectList.get(5));
System.out.print(" " + objectList.get(6) + " =");
output:
This starts to look like calculations:
1 + 3432.123 =
3432.123 * 4535 =
4535 - 24.4 =
private String buildOperation(int pos){
String output;
if(pos == 0) {
output = "+";
}else if(pos == 1){
output = "*";
}else {
output = "-";
}
return output;
}
List<Object> objectList = new ArrayList(res);
for(int i = 0; i < objectList.size()-1; i++){
System.out.println(objectList.get(i) + buildOperation(i) + objectList.get(i+1) + "=");
}
Additionaly I'll use a HashMap with the operations to avoid all if/else conditions
Map<Integer,String> operations = new HashMap{}
operations.put(0,"+");
operations.put(1,"*");
operations.put(2,"-");
System.out.println(objectList.get(i) + operations.get(i) + objectList.get(i+1) + "=");
}
Final solution now the String size does not matter anymore.
ArrayList<Object> objectList = new ArrayList<Object>(res);
System.out.print("\n\nThis starts to look like calculations:");
int maxi= objectList.size();
maxi = maxi -2;
System.out.println("\n\nmaxi = " + maxi);
for (int i = 0; i < maxi; i+=2) {
System.out.println("");
System.out.println(i);
System.out.print("\n\n" + objectList.get(i));
System.out.print(" " + objectList.get(i + 1));
System.out.print(" " + objectList.get(i + 2)+ " =");

Java for loop to count up output

I have a working code but my output doesn't count up.
Here is the code I am working with:
for(Course course : courses) {
for(int i=0;i<1;i++) {
System.out.println("[" + (i+1) + "]" + course.getCode() + "(" + course.getCreditHour() + ")");
}
}
System.out.print("Enter your choice : ");
I need the (i+1) to count from one to 7.
Here is a copy of the output I currently get:
Please type the number inside the [] to register for a course
The number inside the () is the credit hours for the course
[1]IT1006(6)
[1]IT4782(3)
[1]IT4789(3)
[1]IT4079(6)
[1]IT2230(3)
[1]IT3345(3)
[1]IT2249(6)
Enter your choice :
I need the numbers inside the square brackets to count from 1 to 7.
This is for an academic assignment.
Your inner loop isn't doing anything. There's no point in using a loop if you've hard coded it to just run once.
I'd get rid of your outer loop and just index courses directly:
for(int i = 0; i < courses.size(); i++){
Course course = courses.get(i);
System.out.println("[" + (i+1) + "]" + course.getCode() + "(" + course.getCreditHour() + ")");
}
for(Course course : courses) means : for each course so i is reinitialised you whant a variable that will be increment on each iteration so the variable must be declared outside the block. you can write some thing like this :
int i = 1;
for(Course course : courses) {
System.out.println("[" + (i++) + "]" + course.getCode() + "(" + course.getCreditHour() + ")");
}
System.out.print("Enter your choice : ");
the method of #Carcigenicate will work too but can handle so performance issue if you use linked structure as linkedlist tthis will become for an Array :
for (int i = 0 ; i < courses.lenght ; i++){
System.out.println("[" + i + "]" + courses[i].getCode() + "(" + courses[i].getCreditHour() + ")");
}
System.out.print("Enter your choice : ");
and on collections :
for (int i = 0 ; i < courses.getSize(); i++){
System.out.println("[" + i + "]" + courses.get(i).getCode() + "(" + courses.get(i).getCreditHour() + ")");
}
System.out.print("Enter your choice : ");

Printing out multiple random results

I'm making a random creature generator, its going all nice and dandy, however when it comes to printing the results, it prints the same result 5 times. I tried some different things like using println() multiple times and do while loops, however every time I run the file I just get a bunch of the same results. "a b c d e" are strings that generate the creature
int x = 1;
do {
System.out.println(x +" " +a +" " +b +" " +c +" " +d +" " +e);
x++;
} while (x<=5);
The reason why you're getting the same answer 5 times is because your do-while loop runs 5 times without changing the 'creatures' .
System.out.println(a +" "+ b + " " + c + " " + d + " " +e);
If you remove the do-while loop you'll get the same answer only once however just in case i misunderstood your question i made a small demo of a simple way in which to get multiple random results with a for-loop,a String-array and the Random class
String[] creatures = {"Dog", "Cat", "Fish", "Monkey", "Horse"};
Random r = new Random();
for (int i = 0; i < 5; i++) {
String creature1 = creatures[r.nextInt(creatures.length)];
String creature2 = creatures[r.nextInt(creatures.length)];
String creature3 = creatures[r.nextInt(creatures.length)];
String creature4 = creatures[r.nextInt(creatures.length)];
String creature5 = creatures[r.nextInt(creatures.length)];
System.out.println(creature1 + " " + creature2 + " " + creature3
+ " " + creature4 + " " + creature5);
}

How do I return the array values despite being inside a nested for loop?

for(int counter = 0; counter < args.length; counter++){
System.out.println("Displaying per words: " + args[counter]);
splitWords = args[counter].toCharArray();
for(int counter2 = 0; counter2 < splitWords.length; counter2++){
System.out.println("Word spliced: " + splitWords[counter2]);
System.out.println("The number equivalent of " + splitWords[counter2] + " is "
+ (int) splitWords[counter2]);
occurenceCount[(int)splitWords[counter2]]++;
System.out.println("The letter " + splitWords[counter2] +
" was shown " + occurenceCount[(int)splitWords[counter2]] + " times.");
}
}
My function doesn't detect counter2 as a variable since it was inside the nested for loop. So how do I get out of this dilemma?
I'm trying to use the argument inputs (string respectively) and post the number of occurrences using an ascii table as reference and, as you see, there's just one obstacle from stopping me from accomplishing that.
Any ideas?
Your primary problem is that you have missed one important fact - your counts are not complete until after your loop has completed.
You therefore need to print out your counts in a separate loop after your first loop is complete.
public void test() {
String[] args = {"Hello"};
int[] occurenceCount = new int[256];
for (int word = 0; word < args.length; word++) {
System.out.println("Displaying per words: " + args[word]);
char[] splitWords = args[word].toCharArray();
for (int character = 0; character < splitWords.length; character++) {
System.out.println("Word spliced: " + splitWords[character]);
System.out.println("The number equivalent of " + splitWords[character] + " is "
+ (int) splitWords[character]);
occurenceCount[(int) splitWords[character]]++;
System.out.println("Word spliced: " + splitWords[character]);
}
}
// Scond loop to print the results.
for (int character = 0; character < occurenceCount.length; character++) {
int count = occurenceCount[character];
if (count > 0) {
System.out.println("The letter " + ((char) character)
+ " was shown " + count + " times.");
}
}
}

How do I output a draw from a vote (In Java)?

I have no idea on how to output a draw from a vote any help on with this would be appreciated.
At the moment it will allow a user to select the amount of candidates and then the user inputs details. Then the user will enter a vote, which is the candidate number, and type 999 to finish. The output will be the winner or winners(draw)the candidates with details and votes and the amount of spolit votes that is votes not in the range declared at the start.
int x;
char highestChar = '1';
char nextHighestChar = '1';
String alpha = "123456";
int largest=intVoteCount[1];
int nextLargest=intVoteCount[1];
for( x=1; x<=range; x++){
if(intVoteCount[x]>largest){
largest = intVoteCount[x];
highestChar = alpha.charAt(intLoopCount);
}
if(intVoteCount[x]>highestChar){
nextLargest = intVoteCount[x];
nextHighestChar = alpha.charAt(intLoopCount);
}
}
System.out.println("The winner is Candidate number "+ highestChar + " with " + largest + " votes.");
System.out.println("The winner is Candidate number "+ nextHighestChar + " with " + nextLargest + " votes.");
System.out.println("-----------------------------");
System.out.println("The Candidate votes are as follows.");
for (intLoopCount = 1; intLoopCount <= range; intLoopCount++) {
// Display all records.
// New Instance
System.out.println("");
System.out.println("Candidate " + intLoopCount + " "
+ strCandidateTitle[intLoopCount] + " "
+ strCandidateFirstname[intLoopCount] + " "
+ strCandidateSurname[intLoopCount] + " votes "
+ intVoteCount[intLoopCount]);
}
System.out.println("-----------------------------");
System.out.println("Vote Count Spolit: " + intVoteCountSpolit);
}
}
If largest == nextLargest you have a draw, so print out an appropriate message saying so; otherwise, you have an explicit winner.

Categories

Resources