Jump to content

I need some serious Java help.

Voids
Go to solution Solved by Samskip,

You should use if(s.equals("Square")).

== compares actual objects, .equals compares the actual string values.

Okay so I have to make a program whereby I if the user types in"Square" they get the boundry length and the area. My main class runs fine (shape.java) but my constructor class (Square.java) does not run at all.

 

If you could kindly read the code and tell me what I'm doing wrong.

 

Shape.java

___________________

 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
package shape;
 
/**
 *
 * @author Gary
 */
import java.util.Scanner;
public class Shape {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
 
                
String s;
 
Scanner input = new Scanner(System.in);
System.out.print("Please select what shape you want: ");
s = input.nextLine();
System.out.print("The shape you chose was: "+ s);
 
if (s == "Square")
        {
           
            Square newSquare = new Square(0, 0);
            newSquare.getBoundryLength(0);
        }
}
 
 
    }
 
__________________________________
 
Square.java
__________________________________
 
/* The following piece of code calculates the Boundry Length and Area of a square
 * Code created by Gary Stewart - B00638884
 * Date created - 04/10/2014
 */
 
import java.util.Scanner;
import java.lang.Math;
import java.lang.Object;
 
public class Square {
 
public int L;
public int boundryLength;
        public int Area;
        
        public Square(int squareBoundryLength, int squareArea)
        {
           boundryLength = squareBoundryLength;
           Area = squareArea;     
        }
// The user will input a length "L" and then the boundry length will be calculate
        
public static void getBoundryLength(int boundryLength)
{
Scanner bL = new Scanner(System.in);
System.out.println("Type in the Length of the Square: ");
int L = bL.nextInt();
boundryLength = 4*L;
System.out.print("The boundry length of a square is: "+ boundryLength);
}
 
// This method will calculate the area of the square
 
public static void getArea(int Area)
{
Scanner aR = new Scanner(System.in);
System.out.println("Type in the length of the Square: ");
//Allows the user to input the length they desire
int L = aR.nextInt();
Area = L*L;
System.out.print("The area of the square is: "+ Area);
}
}
 
    
 
Link to comment
Share on other sites

Link to post
Share on other sites

You should use if(s.equals("Square")).

== compares actual objects, .equals compares the actual string values.

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

Use the code tag, please

Anyway, the methods in Square shouldn't be static

Static means that it's not bound to any particular instance (i.e. The 'this' keyword has no meaning in a static function, as there is no 'this')

Edit: There are many errors, you may want to start with a simpler program and just make it work, then step up to this

Link to comment
Share on other sites

Link to post
Share on other sites

if (s == "Square") isn't correct. 

 

Use if(s.equals("Square")) .....

 

I'd suggest adding some print lines to see where the code fails. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Personally, unless it's required, I wouldn't ask for input in the Square methods. I'd ask for the input inside the if statement in your Shape classes main method. It would remove code duplication in your Square class and allow them to just calculate the information based on the parameters. Then just have those methods return the value and have the Shape class print it out.

 

Something like this

public class Shape {    public static void main(String[] args) {          String s;        Scanner input = new Scanner(System.in);        System.out.print("Please select what shape you want: ");        s = input.nextLine();        System.out.print("The shape you chose was: "+ s);        if (s.equals("Square"))        {            //Allow the user to input the length they desire            Scanner aR = new Scanner(System.in);            System.out.println("Type in the length of the Square: ");            int L = aR.nextInt();            System.out.println("The boundry length of the square is: " + Square.getBoundryLength(L));            System.out.println("The area of the square is: "+ Square.getArea(L));        }    }}

And

public static class Square {    // This method will calculate the boundry length of the square           public static int getBoundryLength(int length)    {        return 4*length;    }    // This method will calculate the area of the square    public static int getArea(int length)    {        return length*length;    }}

But if you can't use a static class like this, then it's a pretty easy switch back with something like this

public static class Square {    public int length;    public Square(int length)    {        this.length = length;         }    // This method will calculate the boundry length of the square           public int getBoundryLength()    {        return 4*length;    }    // This method will calculate the area of the square    public int getArea()    {        return length*length;    }}

Then in the Shape classes if statement, instead of using

System.out.println("The boundry length of the square is: " + Square.getBoundryLength(L));System.out.println("The area of the square is: "+ Square.getArea(L));

You'd do

Square s = new Square(L);System.out.println("The boundry length of the square is: " + s.getBoundryLength());System.out.println("The area of the square is: "+ s.getArea());

Hope that helps. I haven't coded in Java in a long time so hope I didn't do anything that's incorrect.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×