Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,368 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,448 people online right now. Registration is fast and FREE... Join Now!




Trying to get the right wording in an array.

2 Pages V  1 2 >  

Trying to get the right wording in an array.

Mariko1222

30 Jun, 2009 - 05:16 PM
Post #1

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 41

CODE

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

User is offlineProfile CardPM
+Quote Post


virgul

RE: Trying To Get The Right Wording In An Array.

30 Jun, 2009 - 06:14 PM
Post #2

D.I.C Head
**

Joined: 18 Mar, 2009
Posts: 178



Thanked: 13 times
My Contributions
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
CODE

//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.
User is offlineProfile CardPM
+Quote Post

pbl

RE: Trying To Get The Right Wording In An Array.

30 Jun, 2009 - 06:26 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,535



Thanked: 1125 times
Dream Kudos: 450
My Contributions
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];


User is online!Profile CardPM
+Quote Post

Fuzzyness

RE: Trying To Get The Right Wording In An Array.

30 Jun, 2009 - 08:26 PM
Post #4

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,145



Thanked: 172 times
Dream Kudos: 150
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

pbl

RE: Trying To Get The Right Wording In An Array.

30 Jun, 2009 - 08:33 PM
Post #5

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,535



Thanked: 1125 times
Dream Kudos: 450
My Contributions
Ya but the first post said

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

whatever... I guess both explanations where clear smile.gif
User is online!Profile CardPM
+Quote Post

cfoley

RE: Trying To Get The Right Wording In An Array.

1 Jul, 2009 - 04:37 AM
Post #6

D.I.C Addict
Group Icon

Joined: 11 Dec, 2007
Posts: 645



Thanked: 60 times
Dream Kudos: 25
My Contributions
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. smile.gif

This post has been edited by cfoley: 1 Jul, 2009 - 04:38 AM
User is offlineProfile CardPM
+Quote Post

Mariko1222

RE: Trying To Get The Right Wording In An Array.

1 Jul, 2009 - 05:13 PM
Post #7

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 41

Been working on this all day. I came up with some more, am I correct so far?
CODE

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
CODE

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
User is offlineProfile CardPM
+Quote Post

pbl

RE: Trying To Get The Right Wording In An Array.

1 Jul, 2009 - 05:23 PM
Post #8

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,535



Thanked: 1125 times
Dream Kudos: 450
My Contributions
CODE

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

double average = total / list.length;

biggrin.gif
User is online!Profile CardPM
+Quote Post

Mariko1222

RE: Trying To Get The Right Wording In An Array.

1 Jul, 2009 - 05:59 PM
Post #9

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 41

QUOTE(pbl @ 1 Jul, 2009 - 05:23 PM) *

CODE

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

double average = total / list.length;

biggrin.gif


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???
User is offlineProfile CardPM
+Quote Post

pbl

RE: Trying To Get The Right Wording In An Array.

1 Jul, 2009 - 06:20 PM
Post #10

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,535



Thanked: 1125 times
Dream Kudos: 450
My Contributions
QUOTE(Mariko1222 @ 1 Jul, 2009 - 05:59 PM) *

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

seems logic to me
User is online!Profile CardPM
+Quote Post

virgul

RE: Trying To Get The Right Wording In An Array.

1 Jul, 2009 - 06:41 PM
Post #11

D.I.C Head
**

Joined: 18 Mar, 2009
Posts: 178



Thanked: 13 times
My Contributions
you dont need this part
CODE
list= new double[20];
also im not sure that doing this will work
CODE
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

CODE
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
User is offlineProfile CardPM
+Quote Post

slydunan

RE: Trying To Get The Right Wording In An Array.

1 Jul, 2009 - 07:40 PM
Post #12

New D.I.C Head
*

Joined: 1 Jul, 2009
Posts: 3

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

[code]
public class Prices
{

User is offlineProfile CardPM
+Quote Post

slydunan

RE: Trying To Get The Right Wording In An Array.

1 Jul, 2009 - 07:50 PM
Post #13

New D.I.C Head
*

Joined: 1 Jul, 2009
Posts: 3

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

CODE

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]);
        }

        }
}

User is offlineProfile CardPM
+Quote Post

Mariko1222

RE: Trying To Get The Right Wording In An Array.

2 Jul, 2009 - 03:57 AM
Post #14

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 41

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..... biggrin.gif
User is offlineProfile CardPM
+Quote Post

slydunan

RE: Trying To Get The Right Wording In An Array.

2 Jul, 2009 - 08:20 AM
Post #15

New D.I.C Head
*

Joined: 1 Jul, 2009
Posts: 3

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:

CODE

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 tongue.gif
User is offlineProfile CardPM
+Quote Post

Mariko1222

RE: Trying To Get The Right Wording In An Array.

2 Jul, 2009 - 06:23 PM
Post #16

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 41

It's me again! Getting sick of me? Hope not.

Here is what I've got....I'm still getting errors when I compile.
CODE

import javax.swing.*;
import java.util.*;
public class Prices
{
    public static void main(String[] args)
    {
        double[] prices = {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};
        double total=0.0;
        for(int i=0; i<prices.length; i++)
        {
           total=total + prices[i];
        }
        double average = total/price.length;
        System.out.println(average);
        if(prices[i]<5.00);
        System.out.println(prices);
    }
}

Here are the errors I keep getting.
Copyright © 2006 Microsoft Corporation. All rights reserved.

C:\Java Working Directory>javac Prices.java
Prices.java:13: cannot find symbol
symbol : variable price
location: class Prices
double average = total/price.length;
^
Prices.java:15: cannot find symbol
symbol : variable i
location: class Prices
if(prices[i]<5.00);
^
2 errors

I've made corrections, but I'm still not getting it to compile. I also can get it to print out the values that are higher than the average.

HELP!

My next class is applets, and so far, I understand that!
Thanks again,
Mary
User is offlineProfile CardPM
+Quote Post

pbl

RE: Trying To Get The Right Wording In An Array.

2 Jul, 2009 - 06:35 PM
Post #17

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,535



Thanked: 1125 times
Dream Kudos: 450
My Contributions
double average = total/price.length;

should be prices.length with an S

if(prices[i]<5.00);

which i
i does not exist out of the previous loop
User is online!Profile CardPM
+Quote Post

Mariko1222

RE: Trying To Get The Right Wording In An Array.

3 Jul, 2009 - 04:03 AM
Post #18

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 41

QUOTE(pbl @ 2 Jul, 2009 - 06:35 PM) *


if(prices[i]<5.00);

which i
i does not exist out of the previous loop



Okay, that what should it be if not i?

How do I get it to print out the prices that are higher than the average?
User is offlineProfile CardPM
+Quote Post

pbl

RE: Trying To Get The Right Wording In An Array.

3 Jul, 2009 - 01:00 PM
Post #19

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,535



Thanked: 1125 times
Dream Kudos: 450
My Contributions
When your Average is calculated
perform another for() loop and prints the prices[i] higher than average
User is online!Profile CardPM
+Quote Post

Mariko1222

RE: Trying To Get The Right Wording In An Array.

3 Jul, 2009 - 02:15 PM
Post #20

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 41

QUOTE(pbl @ 3 Jul, 2009 - 01:00 PM) *

When your Average is calculated
perform another for() loop and prints the prices[i] higher than average


okay, a few NEW problems.....
1] It makes a line each time it adds another number...is this okay?

2] It won't let me say anything, it just prints the average.

3] It won't show the values less than 5.oo

4] It won't printout the calculated average.

So once again, here is my current coding...
CODE

import javax.swing.*;
import java.util.*;
public class Prices
{
    public static void main(String[] args)
    {
        double[] prices = {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};
        double total=0.0;
        for(int i=0; i<prices.length; i++)
        {
           total=total + prices[i];
           System.out.println("The sum of the prices are: " + total);
        }
        double average = total/prices.length;
        System.out.println(average);
        }
}

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:54PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month