3 Replies - 57022 Views - Last Post: 06 March 2008 - 01:51 PM Rate Topic: -----

#1 jmprgrl74  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-March 08

Passing an Array

Post icon  Posted 06 March 2008 - 01:16 PM

Any help you could give me with this is very much appreciated. I have moved the code all over the place, changed it a dozen times and am still having issues. I'll paste the code and error I'm receiving. The point is to pass two arrays to two methods that sort them by name and price depending on users' input.

import javax.swing.*;
public class Item
{
	public static void main(String[] args)
	{
		String sort = "";
		String nSort = "";
		double pSort = 0;
		final int NUM_ELEMENTS = 5;
		String[] itemName = {"Coffee", "Cappuccino", "Hot Chocolate", "Bagel", "Donut"};
		double[] itemPrice = {1.00, 2.00, 1.50, 1.25, 0.75};
		sort = JOptionPane.showInputDialog(null, "Welcome to Pams Coffee Shop.  We have a great list of items on our menu. Would you like to see these items sorted by name or by price? (n/p)");
		if(sort == "n")
			{
			nSort = nameSort(itemName, NUM_ELEMENTS); //call method that sorts by name
			JOptionPane.showMessageDialog(null, nSort); // show names
			}
		else
			{
			pSort = priceSort(itemPrice, NUM_ELEMENTS);
			JOptionPane.showMessageDialog(null, pSort); // show prices
			}

	System.exit(0);	
	}
	

	public static void nameSort(Item[] array, int len)
	{
		int a, b;
		Item temp;
		int highSubscript = len - 1;
		for(a = 0; a < highSubscript; ++a)
			for(b = 0; b < highSubscript; ++b)
				if (array[b].itemName() > array[b+ 1].itemName())
				{
					temp = array[b];
					array[b] = array[b + 1];
					array[b + a] = temp;
				}
	}
	
	public static void priceSort(Item[] array, int NUM_ELEMENTS)
	{
		int a, b;
		Item temp;
		int highSubscript = len - 1;
		for(a = 0; a < highSubscript; ++a)
			for(b = 0; b < highSubscript; ++b)
				if (array[b].itemPrice() > array[b+ 1].itemPrice())
				{
					temp = array[b];
					array[b] = array[b + 1];
					array[b + a] = temp;
				}
	}
}



The errors: (I had it to just a couple, then changed things and made it worse apparently.)

Quote

----jGRASP exec: javac -g C:\Documents and Settings\pll\Desktop\Kaplan\Java\Item.java

Item.java:15: nameSort(Item[],int) in Item cannot be applied to (java.lang.String[],int)
nSort = nameSort(itemName, NUM_ELEMENTS); //call method that sorts by name
^
Item.java:20: priceSort(Item[],int) in Item cannot be applied to (double[],int)
pSort = priceSort(itemPrice, NUM_ELEMENTS);
^
Item.java:35: cannot find symbol
symbol : method itemName()
location: class Item
if (array[b].itemName() > array[b+ 1].itemName())
^
Item.java:35: cannot find symbol
symbol : method itemName()
location: class Item
if (array[b].itemName() > array[b+ 1].itemName())
^
Item.java:47: cannot find symbol
symbol : variable len
location: class Item
int highSubscript = len - 1;
^
Item.java:50: cannot find symbol
symbol : method itemPrice()
location: class Item
if (array[b].itemPrice() > array[b+ 1].itemPrice())
^
Item.java:50: cannot find symbol
symbol : method itemPrice()
location: class Item
if (array[b].itemPrice() > array[b+ 1].itemPrice())
^
7 errors


Thank you!

This post has been edited by jmprgrl74: 06 March 2008 - 01:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing an Array

#2 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Passing an Array

Posted 06 March 2008 - 01:30 PM

A few things to start with:

- your methods nameSort and priceSort are expecting and array of Item
as first parameter. You are calling the first one with an array of String
and the second one with an array of double
- these two methods are declared void so they cannot return anything

You probably want to change nameSort to receive an array of String and priceSort to receive an array of double and just call them:

nameSort(itemName, NUM_ELEMENTS);

without

something = nameSort(....)

actually you do not need NUM_ELEMENTS

void nameSort(Sting[] items) {

int nb = items.length();
Was This Post Helpful? 0
  • +
  • -

#3 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Passing an Array

Posted 06 March 2008 - 01:35 PM

public class Item
{
    public static void main(String[] args)
    {
        String sort = "";
        String nSort = "";
        double pSort = 0;
        final int NUM_ELEMENTS = 5;
        String[] itemName = {"Coffee", "Cappuccino", "Hot Chocolate", "Bagel", "Donut"};
        double[] itemPrice = {1.00, 2.00, 1.50, 1.25, 0.75};
        sort = JOptionPane.showInputDialog(null, "Welcome to Pams Coffee Shop.  We have a great list of items on our menu. Would you like to see these items sorted by name or by price? (n/p)");
        if(sort == "n")
            {
            nSort = nameSort(itemName, NUM_ELEMENTS); //call method that sorts by name
            JOptionPane.showMessageDialog(null, nSort); // show names
            }
        else
            {
            pSort = priceSort(itemPrice, NUM_ELEMENTS);
            JOptionPane.showMessageDialog(null, pSort); // show prices
            }

    System.exit(0);    
    }
    

    public static String nameSort(String[] array, int len)
    {
        int a, b;
        String temp;
        int highSubscript = len - 1;
        for(a = 0; a < highSubscript; ++a)
            for(b = 0; b < highSubscript; ++b )
                if (array[b].compareToIgnoreCase(array[b+ 1]) > 0)
                {
                    temp = array[b];
                    array[b] = array[b + 1];
                    array[b + a] = temp;
                }
        return array[0];
    }
    
    public static double priceSort(double[] array, int len)
    {
        int a, b;
        double temp;
        int highSubscript = len - 1;
        for(a = 0; a < highSubscript; ++a)
            for(b = 0; b < highSubscript; ++b )
                if (array[b] > array[b+ 1])
                {
                    temp = array[b];
                    array[b] = array[b + 1];
                    array[b + a] = temp;
                }
        return array[0];
    }
}




This should remove your errors but it is not the solution to what you want the program to do.

This post has been edited by letthecolorsrumble: 06 March 2008 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 jmprgrl74  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-March 08

Re: Passing an Array

Posted 06 March 2008 - 01:51 PM

Thank you guys so much! Unfortunately that didn't work, Let, but thanks for the tips guys, I'll keep at it from what you could give me.

Pamela

This post has been edited by jmprgrl74: 06 March 2008 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1