Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Trouble with Arrays

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with Arrays

    I am trying to complete a program which takes in user input for 6 locations along with donation amounts for each. I then need to print out the site location with the largest cash donation. I figured out how to print out the actual dollar amount, but how do I code to see what the site with that amount is? Here is what I have:

     
    import java.util.Scanner; 
     
    public class Asg06 { 
     
    		static double [] cashDonations = new double[6];  
    		static double [] lbsFood = new double[6];  
    		static String [] siteName = new String[6];  
    		static String bestSiteCash = " ";  
    		static String bestSiteFood = " ";  
    		static double totalCash = 0;  
    		static double totalFood = 0;  
    		static double maxFood = 0;  
    		static double maxCash = 0;  
     
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in); 
    		String anotherCourse = "yes";
    		do {
     
    			getDonations();
    			processDonations();
    			displayDonations();			
     
    			System.out.print("Enter yes if you want to run again: ");
    			anotherCourse = input.next();
    			System.out.print("\n\n\n");
    		} while (anotherCourse.equalsIgnoreCase("yes"));
     
    	} // end of main
     
    	public static void getDonations()
    	{
    		Scanner input = new Scanner(System.in); //needed for input
    		for (int i = 0; i < siteName.length; i++)
    		{
    			System.out.println("Enter site location name: ");
    			siteName[i] = input.next();
    			input.nextLine();
    			System.out.println("");
    			do{
    			System.out.println("Enter cash donations for " + siteName[i] + " ");
    			cashDonations[i] = input.nextDouble();
    			if(cashDonations[i] < 0)
    			{
    				System.out.println("ERROR---Please Enter Only Positive Numbers!!!");
    			}//end if
    			System.out.println("");
    			}while(cashDonations[i] < 0);//end while
     
    			do{
    			System.out.println("Enter lbs of food donations for " +siteName[i] + " ");
    			lbsFood[i] = input.nextDouble();
    			if(lbsFood[i] < 0)
    			{
    				System.out.println("ERROR---Please Enter Only Positive Numbers!!!");
    			}//end if
    			System.out.println("");
    			}while(lbsFood[i] < 0 );//end while
    			}//end of for 
     
    	} //end of getDonations()
     
    	public static void processDonations()
    	{
    		maxCash = cashDonations[0];
    		maxFood = lbsFood[0];
     
    		totalCash = 0;
    		totalFood = 0;
    		for(int i = 0; i < siteName.length; i++)
    		{
    			totalCash = totalCash + cashDonations[i];
    			totalFood = totalFood + lbsFood[i];
    		}
     
    		for (int i = 0; i < cashDonations.length; i ++)
    		{
    			if (cashDonations[i] > maxCash)
    				maxCash = cashDonations[i];
    		}//end of for loop
     
    		for (int i = 0; i < lbsFood.length; i ++)
    		{
    			if (lbsFood[i] > maxFood)
    				maxFood = lbsFood[i];
    		}//end of for loop
     
    	}//end of processDonations()
     
    	public static void displayDonations()
    	{
    		System.out.println("Holiday Donation Locations Report");
    		System.out.println("---------------------------------");
    		System.out.println("");
    		for(int i = 0; i < siteName.length; i++){
    		System.out.println("Site: " + siteName[i]);
    		System.out.println("Individual Cash Donations " + cashDonations[i]);
    		System.out.println("Individual Food Donations " + lbsFood[i]);
    		System.out.print("\n\n");
    		}//end of for
    		System.out.println("");
    		System.out.println("Holiday Donation Totals Report");
    		System.out.println("------------------------------");
    		System.out.println("");
    		System.out.printf("Total Cash Donations $%.2f\n", totalCash);
    		System.out.printf("Total Food Donations %.2f lbs\n", totalFood);
    		System.out.println("\n\n");
    		System.out.println("Holiday Donation Best Site Report");
    		System.out.println("---------------------------------");
    		System.out.println("");
    		System.out.println("Best Location For Cash Donation: " + bestSiteCash);
    		System.out.printf("Max Cash Collected Was: $%.2f\n ", maxCash );
    		System.out.println("");
    		System.out.println("Best Location For Food Donation: " + bestSiteFood);
    		System.out.printf("Max Food Collectd Was: %.2f lbs\n ", maxFood );
    		System.out.println("");
     
    	}//end of displayDonations()
     
    }// end of class

    Thanks for your help!
    Last edited by darren102; February 27th, 2014 at 07:06 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with Arrays

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    If you are using parallel arrays, when you find the index in one array for the amount, that same array can be used to get the location from the other array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with Arrays

    I guess I do not understand how to do that. All of the code I try to do what you said gives me an error due to one being a string (siteName) and the other being a double (maxCash).

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with Arrays

    gives me an error
    Please copy the full text of the error message and paste it here. Also post the code that gives the error.

    When the code finds a value it wants to save in one array, it needs to save the array's index for that value to use to access the other array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with Arrays

    Incompatible operand types String and double
    for (int i = 0; i < cashDonations.length; i ++)
    		{
    			if (siteName[i] == cashDonations[i])
     
    		}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with Arrays

    if (siteName[i] == cashDonations[i])
    What is the purpose of that comparison?

    Did the code in post#1 work? Did it find the max value in one array?
    Can it save the index to that max value so it can use that index in the second array?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with Arrays

    Yes, the code works in post#1. Yes, it did find the max value in one array. I guess if it can, I do not know how.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with Arrays

    Take the code in post#1 and change it to save the index into the array where the value that is stored into the max value came from. Every time max is changed, save the index. At the end of the loop, max will have the largest value and the saved index will be the location in the array where max came from.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with Arrays

    ok. one sec.

Similar Threads

  1. Getting trouble using IO
    By JavaRedBeat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2013, 08:40 PM
  2. Unsorted Arrays having trouble controlling the array.
    By orbin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2012, 11:20 AM
  3. Having trouble splitting a text file into multiple arrays.
    By orbin in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 19th, 2012, 05:27 PM
  4. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  5. [SOLVED] having a little trouble
    By XSmoky in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 6th, 2011, 08:48 PM