I am stuck with my Java Arrays program. I am supposed to create a program that a
ID: 441320 • Letter: I
Question
I am stuck with my Java Arrays program. I am supposed to create a program that allows the user to 1) enter in a user id from 1-10 and enter in the # of boxes sold. A user can enter a user's id more than once and the number of boxes sold is unknown.
My output should look like the following example:
ID Boxes Sold
2 4
2 3
1 2
The output process is supposed to be achieved by using a do...while loop < = I have yet to figure out how to make that work properly and have removed it from my code below.
In addition, I have to total the number of boxes sold for each ID and see who has the greatest number sold. I am supposed to then claim that ID number as the winner.
Right now my code will ask for the ID # and the number of boxes sold, but it is not keeping a running total for each array.
I seriously need help. I've looked at so many text books and all of them glaze over parallel arrays.
import java.util.Scanner;
public class Cookies {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
int[] boxesSold = new int [10];
int idNumber, boxes = 0;
int maxBoxes = 0, winner = 0;
System.out.println("How Many ID #s: ");
int count = sc.nextInt();
int[] idNumbers = new int[count];
for (int i = 0; i < count; i++)
{
System.out.println("Enter ID #: ");
idNumbers[i] = sc.nextInt();
System.out.println("Enter boxes sold: ");
boxesSold[i] = sc.nextInt();
}
for(int i = 0; i < idNumbers.length; i++) {
}
System.out.println("The winning class is #" + idNumbers[winner] + " with " + boxesSold[winner]+ " boxes sold!");
}
Explanation / Answer
//I see you are still having difficulty with this problem.
//posting here because dropbox acting up
//comment if you have any questions
//( o_o)
import java.util.Scanner;
public class Cookies {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
//parallel arrays
int[] boxesSold = new int [10];
int[] idNumbers = new int[10];
int people=0;//counts number of actual different id
int idNum, boxes = 0;
int winner = 0;//index of winner
int index;
System.out.println("How Many ID #s: ");
int count = sc.nextInt();
for (int i = 0; i < count; i++){
//get input
System.out.println("Enter ID #: ");
idNum = sc.nextInt();
do{//do not allow negative boxes sold
System.out.println("Enter boxes sold: ");
boxes = sc.nextInt();
if(boxes<0){
System.out.println("The number of boxes sold must be positiive.");
}
}while(boxes<0);
//process input
index=foundAt(idNumbers,people,idNum);
if(index==-1){//not found
idNumbers[people]=idNum;
boxesSold[people]=boxes;
people++;
}
else{ //found
boxesSold[index]+=boxes;
}
}
for(int i = 1; i < people; i++) {
if(boxesSold[winner]<boxesSold[i]){
winner=i;
}
}
System.out.println("The winning class is #" + idNumbers[winner] + " with " + boxesSold[winner]+ " boxes sold!");
}//end main
public static int foundAt(int arr[],int size,int target){//returns index target found at or -1 if not found
for(int i=0;i<size;i++){
if(arr[i]==target){
return i;
}
}
return -1;
}
}//end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.