Reputation: 307
Trying to take an arraylist of numbers and print out the following...
MOST POPULAR NUMBERS
The following numbers were picked 263 times: 41
LEAST POPULAR NUMBERS
The following numbers were picked 198 times: 20
AVERAGE
The Average was 228.545455 times.
The following numbers were picked 228 times: 5 22
The following numbers were picked 229 times: 2 7 12 40
My code...
import java.util.*;
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
public class Hmwk {
public static void main(String[] args) throws FileNotFoundException {
Scanner input=new Scanner (new File ("input.txt"));
int counter = 0;
ArrayList<Integer> numberList = new ArrayList<Integer>(45);
while(input.hasNextInt()){
int in = input.nextInt();
numberList.add(in);
counter++;
}
mostPopular(numberList,counter);
leastPopular(numberList,counter);
average(numberList,counter);
}
public static void mostPopular(ArrayList<Integer> list, int total){
Collections.sort(list);
int popular = 0;
int counter = 0;
int counterTwo = 0;
for (int i=0; i<total-1; i++){
while(list.get(i) == list.get(i+1)){
counter++;
i++;
if(i == total-1) break;
}
if(counter > counterTwo){
counterTwo = counter;
popular = i;
}
}
System.out.printf("MOST POPULAR NUMBERS");
System.out.printf("The following number was picked",counterTwo,"times:", popular);
}
public static void leastPopular(ArrayList<Integer> list, int total){
Collections.sort(list);
int unpopular=0;
int counter = 0;
int counterTwo = 0;
for (int i=0; i<total-1; i++){
while(list.get(i) == list.get(i+1)){
counter++;
i++;
if(i == total-1) break;
if(counter < counterTwo){
counterTwo = counter;
unpopular = i;
}
}
}
System.out.printf("LEAST POPULAR NUMBERS");
System.out.printf("The following number was picked",counterTwo,"times:", unpopular);
}
public static void average(ArrayList<Integer> list, int total){
int sum = 0;
int counter = 0;
ArrayList<Integer> average = new ArrayList<Integer>(45);
for (int i=0; i<total-1; i++){
while(list.get(i) == list.get(i+1)){
counter++;
i++;
if(i == total-1) break;
}
average.add(counter);
}
for (int i = 0; i <average.size(); i++){
sum+= average.get(i);
}
double average2 = sum/total;
System.out.printf("AVERAGE");
System.out.printf("The Average was",average,"times.");
double ceiling = Math.ceil(average2) ;
double floor = Math.floor(average2);
int counter2 = 0;
Collections.sort(list);
for (int i=0; i<total-1; i++){
while(list.get(i) == list.get(i+1)){
counter2++;
i++;
if(i == total-1) break;
}
if(counter2 == ceiling){
System.out.printf("The following number was picked", ceiling,"times:",i);
}
if (counter2 == floor){
System.out.printf("The following number was picked", floor,"times:",i);
}
}
}
My output is currently...
MOST POPULAR NUMBERSThe following number was pickedLEAST POPULAR NUMBERSThe following number was pickedAVERAGEThe Average was
What I can't seem to figure out is where I went wrong in my program, or if I'm making some stupid mistakes while trying to print out the data. Any and all help is much appreciated, thanks for your time.
Upvotes: 0
Views: 138
Reputation: 94499
The manner in which printf
is invoked shows you may have some misconceptions about how it works. printF
does NOT concatenate each argument passed to the method. It takes a String
containing placeholders as the first parameter and then successive arguments to be assigned to each placeholder.
To invoke printf
you need to add placeholders in your initial string and supply the arguments for each placeholder as follows:
System.out.printf("The following number was picked %s times %s",counterTwo, popular);
The code is currently only printing the first String
argument which does not have any place holders. The extra arguments supplied to the method are then ignored because no placeholders exist for these arguments to be assigned.
Here is a simple example to help
String stringForFormatting = "Argument %s Argument %s";
String argument1 = "1";
String argument2 = "2";
System.out.printf(stringForFormatting, argument1, argument2); //any other args would be ignored
//outputs Argument 1 Argument 2
It also appears that you would like the output to appear on different lines. This can be done in two ways.
First you could add \n
to the String
:
System.out.printf("MOST POPULAR NUMBERS\n");
But since you really don't need to use printF
if the String
does not contain any dynamic content (meaning it does not need placeholders), you could use plain System.out.println()
, which will add the newline for you:
System.out.println("MOST POPULAR NUMBERS");
Upvotes: 6