Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Help? Why aren't my if statements working?

  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Help? Why aren't my if statements working?

    I am attempting to create a calculator for calculating the area of a triangle, rectangle or circle.
    I have made each shape into an object, which implements a Shape interface. This is an exercise that is intended to apply the concepts of interface, so I need to keep this portion of the code this way.

    I want to make it so when the user inputs the name of a shape, the program will construct an object and then calculate using the area and perimeter methods for each object. Usually I would create an Array of objects, then calculate using a for loop. However, I want the user to be able to enter input an endless number of times, and I do not really know how else to do this (have an expanding array) aside from using ArrayList, which I don't have much experience using.

    However, the code only adds to the ArrayList and does not execute the if statements afterwards.

    Triangle:
    Java Code:
    public class Triangle implements Shape{
    	private double a;
    	private double b;
    	private double c;
    	
    	public Triangle(double a, double b, double c){
    		this.a = a;
    		this.b = b;
    		this.c = c;
    	}
    	
    	public double area(){
    		double s = (a + b + c)/2.0;
    		return Math.sqrt(s * (s-a)*(s-b)*(s-c));
    	}
     	public double perimeter(){
     		return a + b + c;
     	}
    }
    Circle
    Java Code:
    public class Circle implements Shape{
    	private double radius;
    	
    	public Circle(double radius){
    		this.radius = radius;
    	}
    	
    	public double area(){
    		return Math.PI*radius*radius;
    	}
     	public double perimeter(){
     		return Math.PI* 2.0 * radius;
     		
     	}
    }
    Rectangle
    Java Code:
    public class Rectangle implements Shape{
    	private double width;
    	private double height;	
    	
    	public Rectangle(double width, double height){
    		this.width = width;
    		this.height = height;
    	}
    	
    	
    	public double area(){
    		return width * height;
    	}
     	public double perimeter(){
     		return 2.0 * (width + height);
     	}
    }
    Java Code:
    import java.util.*;
    public class ShapeTest2 {
    	double width;
    	double height;
    	public static void main(String[] args){
    	ArrayList<Object> shapes = new ArrayList<Object>();
    	Scanner input = new Scanner(System.in);{
    	
    	System.out.println("Welcome to Shape Area/Perimeter Calculator!");
    	System.out.println("Enter the type of shape you want to calculate for.");
    	System.out.println("Types of shapes you may pick from: Triangle, Rectangle, Circle");
    		
    	while (input.hasNext()){
    		shapes.add(input.next());
    		System.out.println("list = " + shapes);
    	
    	if ((input.next()).equals("Rectangle")){
    		Shape[] shapes1 = new Shape[1];
    		Scanner tokens = new Scanner(input.nextLine());
            tokens.useDelimiter(",");
            shapes1[0] = new Rectangle(tokens.nextInt(), tokens.nextInt());
            System.out.println("Enter the coordinates for your shape. ex: (10, 30, 40)");
            
            for (int i = 0; i < shapes1.length; i++){
        		if (i == 0){
        			System.out.println("Rectangle area = " + shapes1[i].area() + 
        					", Rectangle perimeter = " + shapes1[i].perimeter());
        			System.out.println();
        		}
    	}
    	if ((input.next()).equals("Triangle")){
    		Shape[] shapes2 = new Shape[1];
    		Scanner tokens1 = new Scanner(input.nextLine());
            tokens1.useDelimiter(",");
            shapes1[0] = new Triangle(tokens1.nextInt(), tokens1.nextInt(), tokens1.nextInt());
            for (int i = 0; i < shapes1.length; i++){
    
        		if (i == 1){
        			System.out.println("Triangle area = " + shapes2[i].area() + 
        					", Triangle perimeter = " + shapes2[i].perimeter());
        			System.out.println();
        		}
    	}
            
    	if ((input.next()).equals("Circle")){
    		Shape[] shapes3 = new Shape[1];
    		Scanner tokens2 = new Scanner(input.nextLine());
            tokens2.useDelimiter(",");
            shapes3[0] = new Circle(tokens2.nextInt());
            for (int i = 0; i < shapes3.length; i++){
        		if (i == 0){
        			System.out.println("Rectangle area = " + shapes3[i].area() + 
        					", Rectangle perimeter = " + shapes3[i].perimeter());
        			System.out.println();
        		}
            }
    	
    	}
    			
    		}	
    					}
    				}
    			}
    	}
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: Help? Why aren't my if statements working?

    Well answer yourself this: why are you calling scanner.next() twice (on line 14 and 17) while you only want to get input once ?

    Seems to me like in that if on line 17 you actually want to check what you just added to the list...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Help? Why aren't my if statements working?

    Oh, that's right. Sorry. Thank you. Let me see if that works.

  4. #4
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Help? Why aren't my if statements working?

    I changed the boolean statement in the if statements to this:
    Java Code:
    if ((shapes.contains("Triangle"))){
    However, it still isn't working.
    After I input "Triangle"
    It prints "list = [Triangle]" and doesn't do anything else. What else should I do?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: Help? Why aren't my if statements working?

    Find out what is wrong and fix it. The number one job of any programmer. If you want help with that you'll need to have more than "it doesn't work"; at the very least post the updated code.

    I want to stress that you must not fall in what I call the "forum trap". You now created a thread, got an answer and you are already well on your way to now abuse this thread to get ALL your problems solved. No, first put some time into your next problem yourself.
    jim829 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Help? Why aren't my if statements working?

    Quote Originally Posted by gimbal2 View Post
    I want to stress that you must not fall in what I call the "forum trap".
    I see that a lot (and as of late more frequently). My own term for it is FDS (Forum Dependency Syndrome). But it is still the same. And it is apparently a hard habit to kick.

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

  7. #7
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Help? Why aren't my if statements working?

    Finding the problem is much easier said than done. I'm not an incredibly logical thinker. It's not just me being lazy, it's just that most problems that do not have a straightforward answer tax me to the extent of my capabilities. The answer may be staggeringly obvious to you, however, for me, it really, really isn't. I'm sorry, the truth is, I really am this stupid.

    I look at the code again and again and I just can't see what the problem is, as it makes logical sense to me. I don't know what you want me to do, besides spend hours looking at something I've already attempted finding the problem for, and making no progress.

    Here is what I have tried, to isolate the problem.
    Java Code:
    import java.util.*;
    public class ShapeTest2 {
    	double width;
    	double height;
    	public static void main(String[] args){
    	Scanner input = new Scanner(System.in);{
    
    	
    	System.out.println("Welcome to Shape Area/Perimeter Calculator!");
    	System.out.println("Enter the type of shape you want to calculate for.");
    	System.out.println("Types of shapes you may pick from: Triangle, Rectangle, Circle");
    		
    
    	if ((input.next()).contains("Rectangle")){
    		Shape[] shapes1 = new Shape[1];
            System.out.println("Enter the coordinates for your shape. ex: 10,30");
            
            if(input.hasNextDouble()) {
        	Scanner tokens = new Scanner(input.nextLine());
            tokens.useDelimiter(",");
            double height = tokens.nextInt();
            double width = tokens.nextInt();
            shapes1[0] = new Rectangle(width, height);
            System.out.println("Rectangle area = " + width * height);
        			System.out.println("Rectangle area = " + shapes1[0].area() + 
        					", Rectangle perimeter = " + shapes1[0].perimeter());
        			System.out.println();
            }else{
            	System.out.println("This is not a valid input. Please try again.");
    				}
    			}
    		}
    	}
    }
    I realized the for loop was unnecessary to process the object, so I removed it.
    I added the
    Java Code:
    System.out.println("Rectangle area = " + width * height);
    to see if it was a problem with the methods from the classes itself.
    The delimiter in the code doesn't seem to be working, as whenever I enter an coordinate, it prints the error message.
    However, this doesn't make any logical sense to me, as the syntax seems to be correct?

    Example:

    Welcome to Shape Area/Perimeter Calculator!
    Enter the type of shape you want to calculate for.
    Types of shapes you may pick from: Triangle, Rectangle, Circle
    Rectangle
    Enter the coordinates for your shape. ex: 10,30
    1,2
    This is not a valid input. Please try again.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: Help? Why aren't my if statements working?

    Your problem is that you want to go too fast and you blame the fact that it won't work on "you having problems". THAT is too easy.

    No, you're just making a mess of things and you're not taking the time to research the APIs and classes you're using. Lack of knowledge and understanding is the root of your problems. You need to fix not only that problem but also the evasive behavior you're showing now and then the rest will eventually start to fall into place.

    Let me make an example.

    if(input.hasNextDouble()) { // double

    double height = tokens.nextInt(); // int

    Why hasNextDouble() when you want to read ints?

    DISCLAIMER: you may now be offended by the tone I'm taking. If so then I'll butt out of this thread and let others take over. I stick to tough love and you clearly need it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Help? Why aren't my if statements working?

    This code doesn't even compile. Do you know why? And why in the world are you using an array of size 1. I can understand some more advanced reasons (such as emulating call by name). But for you all it does is make your syntax more complicated.

    Regards,
    Jim
    Last edited by jim829; 02-05-2014 at 02:49 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: Help? Why aren't my if statements working?

    Quote Originally Posted by jim829 View Post
    I can understand some more advanced reasons (such as emulating call by name).
    That went right over my head!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Help? Why aren't my if statements working?

    What exactly? His use of a 1x1 array? Or using an array to change parameters via the argument list? Or did I say something stupid? Perhaps I should have said pass values by name.

    Edit: After some thought, call by name was an inappropriate description. A better analogy would be emulating C's pointer capability so you can return values via the parameter list.

    Regards,
    Jim
    Last edited by jim829; 02-05-2014 at 09:58 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 12-04-2013, 09:32 PM
  2. Why are my if statements not working?
    By icykum in forum New To Java
    Replies: 4
    Last Post: 10-07-2012, 09:27 AM
  3. if statements not working
    By pizzadude223 in forum New To Java
    Replies: 6
    Last Post: 08-01-2010, 06:05 PM
  4. Replies: 6
    Last Post: 11-12-2009, 10:50 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
  •