Results 1 to 11 of 11
  1. #1
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default logical error with my program, don't know why!

    hey so here's my code:
    Java Code:
    import java.util.Scanner;
    
    class Cellphone {
    	
    	private String brand;
    	private long serialNumber;
    	private double Price;
    	
    	public Cellphone (String br, long sN, double Pr)
    	{
    		brand= br;
    		serialNumber = sN;
    		Price = Pr;
    	}
       public Cellphone(Cellphone aCellphone)
       {
    	   this(aCellphone.getbrand(), aCellphone.getserialNumber(), aCellphone.getPrice());
    	   }
       public String getbrand()
       {
    	   return brand;
       }
       public long getserialNumber()
       {
    	   return serialNumber;
       }
       public double getPrice()
       {
    	   return Price;
       }
       public void setBrand(String cellphoneBrand)
    	{
    	     
    	     //allows to set the brand of the cellphone
    		brand = cellphoneBrand;
    	}
    	public void setSerialNumber(long SN)
    	{
    	     // Sets the Serial Number of the cellphone
    	     
    		serialNumber = SN;
    	}
    	public void setPrice(double pr)
    	{
    	     // Sets the price of the cellphone
    	     
    		Price = pr;
    	}
    	public String toString()
    	{
    	    return this.brand + ", " + this.serialNumber + " " + this.Price;
    
    	}
    	public boolean equals(Cellphone phone)
    	{
    		if (Price == phone.Price  && brand.equals(phone))
    			return true;
    		else
    			return false;}
    	public boolean equals2(Cellphone phone)
    	{ if (Price == phone.Price)
    		return true;
    	else 
    		return false; 
    	}
    	public boolean equals3(Cellphone phone)
    	{ if (brand.equals(phone));
    	    return true;
    	}
    	
    }
    
    
    
    public class CellPhoneSearch {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
        Cellphone[] cellphoneArr = new Cellphone[10];
                cellphoneArr [0] = new Cellphone ("Samsung", 123456789, 500.5);
                cellphoneArr [1] = new Cellphone ("HTC", 123459876, 850.3);
                cellphoneArr [2] = new Cellphone ("Sony", 543216789, 1230.4);
                cellphoneArr [3] = new Cellphone ("Acer", 987654321, 600);
                cellphoneArr [4] = new Cellphone ("Razr", 543298761, 700);
    	        cellphoneArr [5] = new Cellphone (cellphoneArr [1]);
    	        cellphoneArr [6] = new Cellphone (cellphoneArr [2]);
    	        cellphoneArr [7] = new Cellphone (cellphoneArr [3]);
    	        cellphoneArr [8] = new Cellphone (cellphoneArr [4]);
    	        cellphoneArr [9] = new Cellphone (cellphoneArr [5]);
    	
    	
    		 System.out.println("Please enter the brand");
    		 Scanner userPreference = new Scanner(System.in);
    		 String userBrand = userPreference.nextLine();
    	   
    		 System.out.println("Please enter the serial number");
    		 Scanner userSN = new Scanner(System.in);
    		 long userSerialNumber = userSN.nextLong();
    		
    	     System.out.println("Please enter the Price");
    	     Scanner userPr = new Scanner(System.in);
    	     Double userPrice = userPr.nextDouble();
    		    
        
    	Cellphone cell_1 = new Cellphone( userBrand, userSerialNumber, userPrice  );
    	Scanner input = new Scanner(System.in);
    	System.out.println("Please enter true or false if you'd like to conduct a search to see whether or not two or more cellphones are similar");
    	boolean enteredValue = input.nextBoolean();
    
    	if (enteredValue == true)
    	   { 
    		for(int i=0; i<=cellphoneArr.length-1; i++)
    	      { if (cell_1.equals(cellphoneArr[i]))
    		   System.out.println(cellphoneArr[i].toString());
    	      }
    		
    	      }
    	else 
    	   for(int i=0; i>=cellphoneArr.length; i++)
    	      {if (cell_1.equals2(cellphoneArr[i]))
    	    	  System.out.println(cellphoneArr[i]);
    	     
    	       }
    	    		   
    		
    	
    
    
    	    }
    	}
    My last if - else statement is suppose to show the information of the match depending on whether or not the person enters true or false. However, when I enter true or false, my program terminates and I have no idea why, any one can help?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: logical error with my program, don't know why!

    It doesn't just terminate; it executes one of those loops and they don't print anything because the test in those loops always fails; sprinkling in a few System.out.println( ... ) statements can clarify why those tests always fail.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: logical error with my program, don't know why!

    What's with the embedded XML? Seems this should be in a different section.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: logical error with my program, don't know why!

    That's the forum software helpfully translating a variable into an acronym tag...
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: logical error with my program, don't know why!

    Quote Originally Posted by Tolls View Post
    That's the forum software helpfully translating a variable into an acronym tag...
    :)
    OK. Thanks! Never seen that before.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: logical error with my program, don't know why!

    It might not be this forum.
    It's possible the code was copy/pasted from another one that inserted that.

    Java Code:
    public class Pr {
       private int pr;
    
      public int getPr() {
        return pr;
      }
    }
    Let's see what that does.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: logical error with my program, don't know why!

    Et voila!

    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: logical error with my program, don't know why!

    What, I didn't notice that, that is weird. I copy pasted it from eclipse.... odd.

    Anyway... it replaced all my Price and Pr by the other codes.

    As for my error. I know my loop is good. It comes from my method but I don't see how my method is wrong. ( I mean I can't see it I know it is wrong.). How could I verify my method in this case? The only thing I can think of that can create this error is the fact that I used (Cellphone phone)

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: logical error with my program, don't know why!

    You did read my reply #2, did you?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: logical error with my program, don't know why!

    You said to insert a system.out.print. Which I did in my loop ( to verify if it runs fine). but where else dd you mean ? in my method?

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: logical error with my program, don't know why!

    Quote Originally Posted by TheRockIng View Post
    You said to insert a system.out.print. Which I did in my loop ( to verify if it runs fine). but where else dd you mean ? in my method?
    Well, if you didn't see what you wanted to see, add more System.out.println( ... ) statements at the right places until you can conclude what's wrong in your code.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Stuck with a logical error
    By Aarix in forum New To Java
    Replies: 10
    Last Post: 01-24-2015, 08:15 PM
  2. Replies: 1
    Last Post: 01-13-2013, 04:55 AM
  3. Stuck on a Do While logical error
    By dougie1809 in forum New To Java
    Replies: 8
    Last Post: 01-23-2012, 05:05 PM
  4. Logical Operator error
    By MBD in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 12:29 PM
  5. Replies: 3
    Last Post: 09-26-2010, 06:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •