Trying to get the right wording in an array.

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1921 Views - Last Post: 04 July 2009 - 06:27 PM Rate Topic: -----

#1 Mariko1222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 19-June 09

Trying to get the right wording in an array.

Post icon  Posted 30 June 2009 - 06:16 PM

public class Prices
{
	 public static void main(String[] args)
	 {
		   



Okay, now would I use...
int[] list = new int [];

and what wording would I use to list each of the twenty prices? Should I put them in parenthesis? int [] list = {2.34, 7.89, 1.39, 8.99, etc......}?

or is there another way to do it?

Please understand that I am trying to write the code, but if you don't understand how to word it, it's difficult to try.

I think with some guidence I can learn this and get through it.
Mary

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to get the right wording in an array.

#2 virgul   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Trying to get the right wording in an array.

Posted 30 June 2009 - 07:14 PM

ok not 100% sure what you mean so ill give you a few examples of what you can do. note that you would need to say double in order for this to work, because you have a decimal
//1
double[] list = new double[20];
//2
double[] list = {23.23., 34.23, 8.3, 5, 23 ....};
//3 - not sure what you need this for so this might apply to you.
double[] list;
//then in your constructor you can have
list = new double[20];
//or
list = new double[size];



hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trying to get the right wording in an array.

Posted 30 June 2009 - 07:26 PM

Virgul statements also apply to int as your first intention seemed to be
int[] list = new int[20];
int[] list = {1, 2, 3};
int[] list; list = new int[10];
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Trying to get the right wording in an array.

Posted 30 June 2009 - 09:26 PM

Indeed they would apply the same way however what Virgul was saying I think is that because she is using Prices - possible decimals. She needs to use an double.
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trying to get the right wording in an array.

Posted 30 June 2009 - 09:33 PM

Ya but the first post said

Okay, now would I use...
int[] list = new int [];

whatever... I guess both explanations where clear :)
Was This Post Helpful? 0
  • +
  • -

#6 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Trying to get the right wording in an array.

Posted 01 July 2009 - 05:37 AM

One more... if you have already declared the array:
int[] list;

This will not compile:
list = {1, 2, 3, 4, 5};

because you can only use the curly bracket notation when you create the array. In this case you can:
list = new int[]{1, 2, 3, 4, 5};

You probably won't find this useful right now but keep it in the back of your mind for when you are working with classes and objects. :)

This post has been edited by cfoley: 01 July 2009 - 05:38 AM

Was This Post Helpful? 0
  • +
  • -

#7 Mariko1222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 19-June 09

Re: Trying to get the right wording in an array.

Posted 01 July 2009 - 06:13 PM

Been working on this all day. I came up with some more, am I correct so far?
public class Prices
{
	public static void main(String[] args)
	{
		  double[] list={2.34, 7.89, 1.34, 8.99, 6.99, 12.50, 5.75, 6.23, 8.54, 7.23, 9.54, 13.83, 1.49, 2.63, 4.27, 3.42, 5.12, 4.69, 9.62, 15.47};

list= new double[20];
System.out.println(prices[]);
	 }
}



Now, my next step is to get this to add all those numbers together and print out the total...OR AM I WRONG???

would I now use the
for(int i=0; i<20; i ++){
num[i] = in.nextDouble();
}
for (int i=0; i<20; i ++){
average=average + num [i];
}System.out.println(average/20);



The second "for" method is to find the average of all the prices.
IS IT CORRECT?????

Would I repead the "for" and average lines again with an i<5.00

As you can see, I'm trying, but I need help!!
Thanks in advance.
Mary
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trying to get the right wording in an array.

Posted 01 July 2009 - 06:23 PM

double total = 0.0;
for(int i = 0; i < list.length; i++)
   total = total + list[i];

double average = total / list.length;


:D
Was This Post Helpful? 0
  • +
  • -

#9 Mariko1222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 19-June 09

Re: Trying to get the right wording in an array.

Posted 01 July 2009 - 06:59 PM

View Postpbl, on 1 Jul, 2009 - 05:23 PM, said:

double total = 0.0;
for(int i = 0; i < list.length; i++)
   total = total + list[i];

double average = total / list.length;


:D


Did I get the other coding right so far?I'm trying desparetly to get this right.

so, I replace my two "for" methods with the one you provided, correct???
Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trying to get the right wording in an array.

Posted 01 July 2009 - 07:20 PM

View PostMariko1222, on 1 Jul, 2009 - 05:59 PM, said:

so, I replace my two "for" methods with the one you provided, correct???

seems logic to me
Was This Post Helpful? 0
  • +
  • -

#11 virgul   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Trying to get the right wording in an array.

Posted 01 July 2009 - 07:41 PM

you dont need this part
list= new double[20];
also im not sure that doing this will work
System.out.println(prices[]);
seeing as you dont have an array named prices, you have one named list.

I think your code (so far) should look like this

public class Prices
{
	public static void main(String[] args)
	{
		  double[] list={2.34, 7.89, 1.34, 8.99, 6.99, 12.50, 5.75, 6.23, 8.54, 7.23, 9.54, 13.83, 1.49, 2.63, 4.27, 3.42, 5.12, 4.69, 9.62, 15.47};

//this is to print out all the values in list
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
	 }
}


then you can add your code. Not sure if you wanted to print all that was in list though, thats your choice ;D
Was This Post Helpful? 0
  • +
  • -

#12 slydunan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-July 09

Re: Trying to get the right wording in an array.

Posted 01 July 2009 - 08:40 PM

but it needs to find the average doesn't it?

[code]
public class Prices
{
Was This Post Helpful? 0
  • +
  • -

#13 slydunan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-July 09

Re: Trying to get the right wording in an array.

Posted 01 July 2009 - 08:50 PM

but it needs to find the average doesn't it?

public class Prices
{
	public static void main(String[] args)
	   {
			  double[] list={2.34, 7.89, 1.34, 8.99, 6.99, 12.50, 5.75, 6.23, 8.54, 7.23, 9.54, 13.83, 1.49, 2.63, 4.27, 3.42, 5.12, 4.69, 9.62, 15.47};

		//find the average (pbl's code)
		double total = 0.0;
		for(int i = 0; i < list.length; i++){
		  total = total + list[i];
		}
		double average = total / list.length;

		//print average
		System.out.println(average);

		//if you need to print the whole list, then virgul's code
		for(int i = 0; i < list.length; i++){
		System.out.println(list[i]);
		}

		}
}


Was This Post Helpful? 0
  • +
  • -

#14 Mariko1222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 19-June 09

Re: Trying to get the right wording in an array.

Posted 02 July 2009 - 04:57 AM

Don't suppose that I need to print the whole list, just want it to show the average.

Okay, next I want it to print out those prices that are below the 5.00. How would I word this?

it's coming..... :D
Was This Post Helpful? 0
  • +
  • -

#15 slydunan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-July 09

Re: Trying to get the right wording in an array.

Posted 02 July 2009 - 09:20 AM

I don't know if you're still finding the average, or if you just want to print out the numbers.
The concept is to just add an if statement (or more if needed), such as this:

if(list[i]<5.00) /*write whatever you need to do here when list[i] is less than 5.00*/;



ill let you figure out where to put this for yourself :P
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2