473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

bad operand '<' I can't compare two Strings

I'm getting a bad operand types for operator <. first String. second string.
I get want the error means, so I'm assuming that I can't use this operator to sort my array of objects. Is there some other way for me to sort this array out. I need to sort the array by the Artist name.
Here's my new code
Expand|Select|Wrap|Line Numbers
  1. public class Product
  2. {
  3.             //DECLARE VARIABLES
  4.     private String number;     //Item Number of product( used String as item 
  5.                    // numbers sometimes contain alphabet characters)
  6.     private String name;      //Name of the product
  7.     private int units;         //How many of the product are in stock
  8.     private double price;      //How much one item costs
  9.     private double value;      //How much the stock is worth
  10.  
  11.  
  12.             //CONSTRUCTOR
  13.     public Product( String productName, String itemNumber, 
  14.        int instockUnits, double costPerUnit, double costOfStock )
  15.     {
  16.         name = productName;
  17.         number = itemNumber;
  18.         units = instockUnits;
  19.         price = costPerUnit;
  20.         value = 0.00;
  21.     }
  22.  
  23.  
  24.             //METHODS OF THE CLASS
  25.     //Set the item number
  26.     public void setNumber( String itemNumber )
  27.     {
  28.         number = itemNumber;
  29.     }
  30.     //End set item number
  31.  
  32.  
  33.     //Get item number
  34.     public String getNumber()
  35.     {
  36.         return number;
  37.     }
  38.     //End get item number
  39.  
  40.  
  41.     //Set name
  42.     public void setName( String productName )
  43.     {
  44.         name = productName;
  45.     }
  46.     //End set name
  47.  
  48.     //Get name
  49.     public String getName()
  50.     {
  51.         return name;
  52.     }
  53.     //End get name
  54.  
  55.     //set units
  56.     public void setUnits( int instockUnits )
  57.     {
  58.         units = instockUnits;
  59.     }
  60.     //end set units
  61.  
  62.  
  63.     //Get units
  64.     public int getUnits()
  65.     {
  66.         return units;
  67.     } 
  68.     //End get units
  69.  
  70.  
  71.     //Set price
  72.     public void setPrice( double costPerUnit )
  73.     {
  74.         price = costPerUnit;
  75.     }
  76.     //End set price
  77.  
  78.  
  79.     //Get price
  80.     public double getPrice()
  81.     {
  82.         return price;
  83.     }
  84.     //End get price
  85.  
  86.  
  87.  
  88.     //Set Value
  89.     public double value( double price, int units )
  90.     {
  91.         return value = price * units;
  92.     }
  93.     //end set value
  94.  
  95.  
  96.     //String output of Product
  97.     public String toString()
  98.     {
  99.         return "Product " + number + "  Type " + name + ".   Cost..." + price + ".   Units..." + units + ".   Value..." + (price * units);
  100.     }
  101.  
  102.  
  103.  
  104. }
Expand|Select|Wrap|Line Numbers
  1. public class CD extends Product   
  2. {
  3.             //Declare variable not inherited
  4.     private String artist;  //name of band or artist
  5.     private final double restockFee = .05;   //5% restocking fee
  6.  
  7.  
  8.  
  9.             //Constructor
  10.     public CD( String productName, String itemNumber, int instockUnits, double costPerUnit, double costOfStock, String artistName )
  11.     {
  12.         super( productName, itemNumber, instockUnits, costPerUnit, costOfStock );
  13.  
  14.         artist = artistName;
  15.     }
  16.  
  17.             //METHODS NOT INHERITED
  18.     //Set artist name
  19.     public void setArtist( String artistName )
  20.     {
  21.         artist = artistName;
  22.     }
  23.     //End set artist name
  24.  
  25.     //Get artist name
  26.     public String getArtist()
  27.     {
  28.         return artist;
  29.     }
  30.     //End get artist name
  31.  
  32.     //calculate new price with restocking fee
  33.     public double price()
  34.     {
  35.         return ( super.getPrice() + ( super.getPrice() * restockFee ) );     
  36.     }    
  37.  
  38.     //Set Value
  39.     public double value()
  40.     {
  41.         return ( price() * super.getUnits() );
  42.     }
  43.     //end set value
  44.  
  45.     //String output of Product
  46.     public String toString()
  47.     {
  48.         return String.format( "%s %s   %s\n%s...%.2f\n\n", "Artist", getArtist(), super.toString(), "Value with Restocking Fee...", ( price() * super.getUnits() ) ); 
  49.     }
  50.  
  51. }
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;  //allows user input
  2. import java.util.Arrays;  //allows us to add to the array
  3. import java.util.Comparator;
  4.  
  5. public class InventoryDisplay
  6. {
  7.     //Main Method
  8.     public static void main( String args[] )
  9.     {
  10.         //Create a product
  11.         CD myCD = new CD( "0000", "Type", 0, 0.00, 0.00, "Band" );
  12.  
  13.         //Create a Scanner
  14.         Scanner input = new Scanner( System.in );
  15.  
  16.         double total = 0.00;
  17.  
  18.         //Greeting
  19.         System.out.print( "Welcome to Inventory Control\n" );
  20.         System.out.print( 
  21.            "This program displays inventory stock and value\n\n" );
  22.  
  23.         //Get the length of the array
  24.         System.out.printf( "%s%s",
  25.            "Before we begin,", 
  26.            "how many CDs do you want to inventory? " );
  27.         int count = input.nextInt();
  28.  
  29.         System.out.printf( "%s%s", "Thank you.", 
  30.            " Please fill in CD information.\n\n" );
  31.  
  32.         //Create an array
  33.         CD inventory[] = new CD[ count ];
  34.  
  35.         //Using inventory.length as a counter, loop input and output
  36.         for ( int counter = 0; counter < inventory.length; counter++ )
  37.         {
  38.             String theName = "CD";  //Product Type is already determined as CD, so no input required
  39.             myCD.setName( theName );
  40.  
  41.             System.out.print( "What is the product number of the CD? ");
  42.             String theNumber = input.next();
  43.             myCD.setNumber( theNumber );
  44.             System.out.println();
  45.  
  46.             System.out.print( "How many of this CD is in stock? ");
  47.             int theUnits = input.nextInt();
  48.             myCD.setUnits( theUnits );
  49.             System.out.println();
  50.  
  51.             System.out.print( "How much does this CD cost? ");
  52.             double thePrice = input.nextDouble();
  53.             myCD.setPrice( thePrice );
  54.             System.out.println();
  55.  
  56.             System.out.print( "Who is the artist of the CD? ");
  57.             String theArtist = input.next();
  58.             myCD.setArtist( theArtist );
  59.             System.out.println();
  60.  
  61.             //Fills the array with the user input
  62.             inventory[ counter ] = new CD( theName, theNumber, theUnits, thePrice, myCD.value( myCD.getPrice(), myCD.getUnits() ), theArtist );
  63.  
  64.  
  65.             //Totals the value of all the items
  66.             /*This snippet of code taken Dream.In.Code
  67.             http://www.dreamincode.net/forums/topic/251302-loop-not-incrementing-my-variable/page__p__1460738__fromsearch__1&#entry1460738
  68.             ChrisKellyDev
  69.             Retrieved October 15th 2011*/
  70.  
  71.             total += inventory[counter].value();
  72.  
  73.              }
  74.  
  75.         System.out.println( "The restocking fee is 5%\n" );
  76.  
  77.         for ( int a = 0; a <= inventory.length; a++ )
  78.         {
  79.             for ( int b = 1; b <= inventory.length; b++ )
  80.             {
  81.                 if ( inventory[a].getArtist() < inventory[b].getArtist() )
  82.                 {
  83.                     CD swapCD = new CD( "0000", "Type", 0, 0.00, 0.00, "Band" );
  84.  
  85.                     swapCD = inventory[a];
  86.                     inventory[a] = inventory[b];
  87.                     inventory[b] = swapCD;
  88.                 }
  89.             }
  90.         }        
  91.  
  92.         System.out.println(Arrays.toString( inventory ) );
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.             System.out.printf( "The total value of the inventory is %.2f\n\n", total );
  101.  
  102.         //Closing
  103.         System.out.print( "Thank you for using Inventory Control" );        
  104.  
  105.     }
  106.  
  107. }
line 86 is the error
Oct 17 '11 #1

✓ answered by Totally Stumped

Never mind folks, someone suggested the compareTo() method and that solved everything

1 2753
Never mind folks, someone suggested the compareTo() method and that solved everything
Oct 17 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: jrc4728 | last post by:
I have a MySQL table with the date stored in three fields as string values like this. (sorry, its imported data) str_yy str_dd str_mm ------------------------ 05 01 04 05 ...
1
by: roy_it | last post by:
During the validation of a xml, with my xsd file, can I compare the value of two attributes? Thank U Nella validazione di un xml, con un file xsd, posso confrontare i valori di due...
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
1
by: David | last post by:
String* str1; String* str2; String* s; str1="Rado"; str2="Robes";
5
by: Tom | last post by:
It appears that you can't compare two dates in DotNet. You must use ToString and compare the strings. Is that the only reliable way? Try this: Dim dteOne As Date =...
2
by: yinglcs | last post by:
I am new to python. How can I compare if 2 files has duplicate entries in python? Is there an example for that? What if the files are big and I don't want to read the whole file in memory. ...
0
by: Diego Martins | last post by:
Hi! The following code snippet: namespace { bool charCompare(char a, char b) { return tolower(a) < tolower(b); } } bool compareString(const std::string & s1, const std::string & s2) {
9
by: Plissken.s | last post by:
Hi, how can i compare a string which is non null and empty? i look thru the string methods here, but cant find one which does it? ...
1
by: vinodjaju | last post by:
How can we compare two xml files in QTP . is there any procedure thru which we can pass parameter in the check point for example am having two xml file that is "medicalnew", and "medical" and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.