Results 1 to 5 of 5
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default Help - passing user input

    Hi guys. I have spent the last hour or so trying to pass a string from user input to match criteria via a else if statement but for some reason it doesnt work. it works fine if i change the tester to use static values..

    Here is my code.

    When i run it it returns the inital value of total.

    Java Code:
    public class Parcel {
    	private double weight;
    	private String destination;
    	
    
    	public Parcel(double weight, String destination){
    		this.weight = weight;
    		this.destination = destination;
    	}
    	public double getWeight(){
    		return this.weight;
    	}
    	public String getDest(){
    		return this.destination;
    	}
    	public double getCost(){
    		double total = 0;
    		
    		if(this.destination == "S")
    			{
    			if (this.weight < 1.0)
    				{
    				total = 4.65;
    				return total;
    				}
    			else if (this.weight >= 1 && this.weight < 2)
    				{
    				total = 4.65;
    				return total;
    				}
    			else if (this.weight >= 2)
    				{
    				total = 4.65;
    				return total;
    				}
    			
    			}
    		else if(this.destination == "M" || this.destination == "B")
    			{
     				if (this.weight < 1.0)
     				{
     					total = 6.55;
     				}
     				else if (this.weight >= 1 && this.weight < 2)
     				{
     					total = 7.0;
     				}
     				else if (this.weight >= 2)
     				{
     					total = 7.45;
     				}
     			}
     		else if(this.destination == "A")
     			{
     				if (this.weight < 1.0)
     				{
     					total = 6.8;
     				}
     				else if (this.weight >= 1 && this.weight < 2)
     				{
     					total = 7.5;
     				}
     				else if (this.weight >= 2)
     				{
     					total = 8.2;
     				}
     			}
     		else if(this.destination == "P")
     			{
     				if (this.weight < 1.0)
     				{
     					total = 8.1;			
     				}
     				else if (this.weight >= 1 && this.weight < 2)
     				{
     					total = 10.10;
     				}
     				else if (this.weight >= 2)
     				{
     					total = 12.10;
     				}
     								
    			}
     		
     		else if(this.destination == "H")
     			{
     				if (this.weight < 1.0)
     				{
     					total = 7.65;
     				}
     				else if (this.weight >= 1 && this.weight < 2)
     				{
     					total = 9.2;
     				}
     				else if (this.weight >= 2)
     				{
     					total = 10.75;
     				}	
     			}
    		return total;
    
    		}
    }
    tester

    Java Code:
    import java.util.Scanner;
    
    public class ParcelTest {
    	public static void main(String[] args){
    		
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("Please enter parcel weight");
    		double weight= scanner.nextDouble();
    		System.out.println("Please enter parcel destination");
    		String destination = scanner.next();
    		System.out.println("You entered " + weight + " " + destination);
    
    		
    		
    		Parcel p1 = new Parcel (weight, destination);
    		//p1.getCost();
    		System.out.println(p1.getDest() + "\n" + p1.getWeight() + "KG\n" + "$" + p1.getCost());
    		
    
    		/*Parcel p2 = new Parcel (1.8, 'B');
    		p2.getCost();
    		System.out.println(p2.getDest() + "\n" + p2.getWeight() + "KG\n" + "$" + p2.getTotal());
    		*/
    		
    
    		
    	}
    }
    Last edited by jonytek; 02-15-2011 at 05:46 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Read the "How would I code this" thread.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I see you really didn't pay much attention to my help in your other thread. This
    Java Code:
    if(this.destination == "S") {
        if (this.weight < 1.0) {
            total = 4.65;
            return total;
        } else if (this.weight >= 1 && this.weight < 2) {
            total = 4.65;
            return total;
        } else if (this.weight >= 2) {
            total = 4.65;
            return total;
        }
    }
    can be reduced to
    Java Code:
    if(....) {
        return 4.65;
    }

  4. #4
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    i did take it on board but for the purpose of the exercise I didnt worry about it. i do understand its good practice to minimise code but I guess I was concentrating on other areas.

    Thanks anyway Junky.

    I appreciate it. Will try the equals method now. hopefully i can squeeze it in before work..

  5. #5
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  2. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  3. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  4. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  5. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM

Posting Permissions

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