Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:
JavaWorld Daily Brew

Need help sorting arrays

Tags:

I am new to java programming and totally lost. Will someone please help me in sorting the arrays in my product class of the following code. Thanks.

import java.util.Scanner;
import java.util.Arrays;
public class Inventory {

public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
boolean isValid = true;//**
String productName;// = "DVD"//++
int productNumber;//++
int onHand;//++
double price;//++
double totalInventory;//++
//int count;
double totInventory;//[] = new double[3];//++
double reStockFee = .05;
double reStockCost = 0;
totalInventory = 0;
int count = 0;

while (isValid)
{
System.out.println ("Enter Product Name. Enter exit to quit program");
productName = input.next();

if (!productName.equalsIgnoreCase ("exit"))//++
{
System.out.println ("Enter Item Number: ");
       productNumber = input.nextInt();
       System.out.println ("Enter number of items in stock: ");
       onHand = input.nextInt();
       System.out.println ("Enter item price: ");
       price = input.nextDouble();
       //compute student grade
       totInventory = (onHand * price);
       reStockCost = (onHand * price * reStockFee);
     //create a new class and pass the values to the method

    Product.getData(productName, productNumber, onHand, price, reStockCost, totInventory, count);//+++
    count++;  //increment the counter to use as the array position in second class//+++
}
else
{
isValid = false;//**

System.out.print ("Item Name" + "\t");

System.out.print ("Item Number" + "\t");

System.out.print ("Items on Hand" + "\t");

System.out.print ("Item Price" + "\t");

System.out.print ("Items Value" + "\t");

System.out.print ("Restock Fee" + "\n");

Product.printArray(count);



Product.modifyArray(pName);
System.out.println ("Thank you, come again.");

break;

}

}

}
}

//Product Class

public class Product {
static int maxlength = 100;
static String pName[] = new String[maxlength];
static int[] iNumber = new int [maxlength];
static int[] iStock = new int [maxlength];
static double[] uPrice = new double [maxlength];
static double[] tInventory = new double [maxlength];
static double [] rStockCost = new double [maxlength];
static double totalInventory = 0;
//static double reStockFee = .05;
//static double reStockCost = 0;
static double totreStock = 0;
//public Product(int productNumber, int onHand, double price,
// double totInventory, int count) {
// TODO Auto-generated constructor stub
//}

public static void getData(String productName, int productNumber, int onHand, double reStockCost, double price, double totInventory, int count)
{
  //check that count doesn't exceed maximum array length
  if (count > maxlength)
  {
   System.out.println("DATA OVERLOAD!!!!. YOU CRASHED THE SYSTEM!!!  Program Exiting!");
   System.exit(0);
  }
  else
  {
   //store values in the array
   pName[count] = productName;
   iNumber[count] = productNumber;
   iStock[count] = onHand;
   uPrice[count] = price;
   rStockCost[count] = reStockCost;
   tInventory[count] = totInventory;
   totalInventory += totInventory;
   totreStock += reStockCost;

   }

   }

//print the values in the array
//public static void printAll(int count)
public static void printArray(int count)
{
for (int i=0; i<count; i++ )
{
System.out.printf(pName[i]);
System.out.printf("%15d", iNumber[i]);
System.out.printf("%18d", iStock[i]);
System.out.printf("%11s$%.2f", "", uPrice[i]);
System.out.printf("%10s$%.2f", "", tInventory[i]);
System.out.printf("%10s$%.2f\n","", rStockCost[i]);
}
  System.out.printf("%s$%.2f\n", "Entire Inventory Value: ", totalInventory);
  System.out.printf("%s$%.2f\n", "Entire Inventory Restock Cost: ", totreStock);


}

}



public class modifyArray
{

public static void modifyArray(pName)
{
Arrays.sort(pName1);
System.out.println("Array contents sfter sort:");
for (int i = 0; i < pName.length; i++)
System.out.println(pName1);
}

}

Your rating: None

Make your objects "Comparable"

You need to implement the Comparable interface (java.util.Comparable) and implement its compareTo() method, this should do the job...

well i will try to solve

well i will try to solve this problem when i get i will soon get back with solution hopefully .

Australia Visa

use

use compareTo method by importing java.util.* package
you will definitely get

i agree with Shailesh

i agree with Shailesh Soliwal :)

Bookmarks Design | Sticker Design | Post Card Design

Comparable interface

Comparable interface (java.util.Comparable) and implement its compareTo() method will resolve the problem

Operating Manual Design | Magazine Design

help

Thank you very much for this information.
Good post thanks for sharing.
I like this site ;)
-----------

promosyon promosyon ürünleri
seo seo danışmanlığı
penis büyütücü penis büyütme hapı
büro mobilyası Ofis ve büro mobilyası
sohbet odaları sohbet odaları
branda Ucuz branda fiyatları
Engelliler Engelliler ve engelli dostlarının paylaşım platformu.
Otomatik kapı Otomatik cam kapı sistemleri
Tercüme Tercüme ve Çeviri
Turkey medical company directory Turkey medical company directory
kız oyunları çoğunlukla kız oyunları oynanabilecek bir oyun sitesi.
-----------

We reported about the launch

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.