d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Displaying Arrays
12Next
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Apr 28 2013 07:26pm
I have an array, that takes in 6 company names, and how much money and food they donated..

I dont know how to display them..


i tried: System.out.println("These are the companies and how much they donated" + siteName[6]"
but my code breaks down.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 28 2013 10:21pm
how about you post your actual code? that's obviously a syntax error, so i'm guessing you didn't copy it correctly. and what datatype is your array? do you have a Company class with properties for name, money, food? or do you have 3 different parallel arrays? or is it a single array with 18 elements and you keep track of it somehow?

and please explain what "my code breaks down" mean. compile error? runtime error? you're getting output you didn't expect?

if you're not using a custom class, then you're prolly using all strings or multiple arrays (string, double, double?). string concatenation should work fine. eg: "my message " + array[1]

if you're using a custom class, make sure you override toString() then do the above

This post was edited by carteblanche on Apr 28 2013 10:23pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 29 2013 07:01am
make your class look like this.

Code
public class Company {
   int money;
   int food;
   public Company( int money, int food ) {
     this.money = money;
     this.food = food;
   }
   @Override
   public String toString( ) {
     return "Money: " + money + " - Food: " + food;
   }
 }


The @Override annotation might give you an error depending on the version of java you're using. You can just delete it if it does

This post was edited by labatymo on Apr 29 2013 07:01am
Member
Posts: 3,451
Joined: Feb 26 2010
Gold: 0.20
Apr 29 2013 07:26am
well..6 company names are indexed 0-5. So array[6] would give u an array out of bounds error?
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Apr 29 2013 08:50am
Quote (silvermace @ Apr 29 2013 07:26am)
well..6 company names are indexed 0-5. So array[6] would give u an array out of bounds error?


That's definitely one error I see too from the one line he gave us.
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Apr 29 2013 09:26am
The chapter is about call methods an single dimension arrays.

these are my global variables;

// GLOBAL VARIABLES
static double [] cashDonations = new double[6];
static double [] lbsFood = new double[6];
static String [] siteName = new String[6];
// GLOBAL CALCULATED VARIABLE
static String bestSiteFood = " ";
static double totalCash = 0;
static double totalFood = 0;
static double maxFood = 0;
static double maxCash = 0;



this is my call method (this is the info i want to display)

public static void getDonations() // CALL METHOD GET DONATIONS

{
Scanner input = new Scanner(System.in);
final int NUMBEROFSITES = 6;
for (int i = 0; i < NUMBEROFSITES; i++)
{
System.out.println(" Site Name: ");
siteName[i] = input.nextLine();

System.out.println(" How much money has been donated by this site? ");
cashDonations[i] = input.nextDouble();

System.out.println("How much food has been donated by this site?");
lbsFood[i] = input.nextDouble();

input.nextLine();


this is my code i started to make to display

System.out.println("-----------------------------------");
System.out.println("\t Overall Donations");
System.out.println("-----------------------------------");



And I want it to look like this..


Holiday Donation Locations Report
---------------------------------

Site: Walgreens
Individual Cash Donations $198.43
Individual Food Donations 14.54 lbs

Site: Walmart
Individual Cash Donations $985.75
Individual Food Donations 243.90 lbs

Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Apr 29 2013 09:43am
Code
System.out.println("Holiday Donation Locations Report\n---------------------------------\n");
for(int i = 0; i < NUMBEROFSITES; i++) {
  System.out.println("Site: " + siteName[i]);
  System.out.println("Individual Cash Donations $" + cashDonations[i];
  System.out.println("Individual Food Donations " + lbsFoos[i] + " lbs");
  System.out.println();
}


as others said, you should rather use a class, if this is an assignment it is really silly... + using global variables is not recommended

This post was edited by m0hawk on Apr 29 2013 09:44am
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Apr 29 2013 09:54am
Quote (m0hawk @ Apr 29 2013 11:43am)
Code
System.out.println("Holiday Donation Locations Report\n---------------------------------\n");
for(int i = 0; i < NUMBEROFSITES; i++) {
  System.out.println("Site: " + siteName[i]);
  System.out.println("Individual Cash Donations $" + cashDonations[i];
  System.out.println("Individual Food Donations " + lbsFoos[i] + " lbs");
  System.out.println();
}


as others said, you should rather use a class, if this is an assignment it is really silly... + using global variables is not recommended


yes, unfourtionatlly it is an assignment :/
i dont know how i am supposed to display it like that..
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Apr 29 2013 10:03am
Quote (Trig @ Apr 29 2013 05:54pm)
i dont know how i am supposed to display it like that..


just use the code I posted ?
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 29 2013 10:19am
You should be using an object for Company as apposed to several arrays to store the data. I'm suprised your teacher didn't teach you about classes before giving you this assignment. Here's how it should look.
Code
import java.text.NumberFormat;
import java.util.Scanner;

public class Donations {

 static class Company {

   String name;

   double money;

   double food;

   NumberFormat currencyFormat;

   public Company( String name, double money, double food ) {
     this.name = name;
     this.money = money;
     this.food = food;
     currencyFormat = NumberFormat.getCurrencyInstance( );
   }

   @Override
   public String toString( ) {

     return name + "Individual Cash Donations "
         + currencyFormat.format( money ) + " Individual Food Donations "
         + food + "lbs";

   }
 }

 private final static int NUMBER_OF_COMPANIES = 6;

 public static void main( String[] args ) {

   Company[] companies = new Company[NUMBER_OF_COMPANIES];

   for ( int i = 0; i < NUMBER_OF_COMPANIES; i++ ) {
     System.out.println( " Site Name: " );
     String siteName = new Scanner( System.in ).next( );
     System.out.println( " How much money has been donated by this site? " );
     double cashDonations = new Scanner( System.in ).nextDouble( );
     System.out.println( "How much food has been donated by this site?" );
     double lblsFood = new Scanner( System.in ).nextDouble( );
     companies[i] = new Company( siteName, cashDonations, lblsFood );
   }
   System.out.println( "Holiday Donation Locations Report"
       + "\n---------------------------------" );

   for ( int i = 0; i < NUMBER_OF_COMPANIES; i++ ) {
     System.out.println( companies[i] );
   }
 }
}


This post was edited by labatymo on Apr 29 2013 10:26am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll