How can I correct this switch case in Java?











up vote
2
down vote

favorite












So I've been working on this code for a while and it has me a bit lost. Keep in mind I am extremely new to Java and so I'm a bit slow on the uptake. I have created shape classes that implement an interface with getArea, getPerimeter and getDescription methods. There are multiple shapes but that isn't really where the problem is. The problem comes when I attempt to implement switch cases to allow the user to choose which shape he or she wants to add. I am getting the same message as many times as the Array of shapes will allow the address of the Shape I try to add. I realize the mistake I'm making is most likely a beginner one by I would really appreciate some help, Thank you. Also if you could give me a clue on how to sort the Shapes by their Area it would be greatly appreciated.



public class ShapeApp2 {

/**
* @param args the command line arguments
*/

public static void main(String args) {
Shape test = new Shape[10];
System.out.println("Choose a shape or type stop to break away?");
Scanner sc = new Scanner(System.in);
String Shape = sc.nextLine();

for (int i=0; i<test.length; i++) {

switch (Shape) {
case "Rectangle":
System.out.println("You have chosen a Rectangle");
test[i] = new Rectangle();
System.out.println("Enter another one now");
break;
case "Square":
System.out.println("You have chosen a Square");
test[i] = new Square();
System.out.println("Enter another one now");
break;
case "Equilateral Triangle":
System.out.println("You have chosen an Equilateral Triangle");
test[i] = new Equilateral_Triangle();
System.out.println("Enter another one now");
break;
case "Right Triangle":
System.out.println("You have chosen a Right Triangle");
test[i] = new Right_Triangle();
System.out.println("Enter another one now");
break;
case "Isosceles Triangle":
System.out.println("You have chosen an Isosceles Triangle");
test[i] = new Isosceles_Triangle();
System.out.println("Enter another one now");
break;
case "Scalene Triangle":
System.out.println("You have chosen a Scalene Triangle");
test[i] = new Scalene_Triangle();
System.out.println("Enter another one now");
break;
case "Stop":
break;
}
System.out.println(test[i]);
}

}


}



Also here's a couple of the Shape Classes for context.



package shapeapp2;

/**
*
* @author my-pc
*/
public class Rectangle implements Shape {
private double length;
private double width;
private String shapeName;
public Rectangle(){
length = 4.0;
width = 5.0;
shapeName = "Rectangle";
}
public double getArea(){
double Area;
Area = length * width;
return Area;
}
public double getPerimeter() {
double Perimeter;
Perimeter = (2*length) + (2*width);
return Perimeter;
}
public String getDescription() {
return shapeName;
}
}



package shapeapp2;

/**
*
* @author my-pc
*/
public class Square implements Shape {
private double length;
private double width;
private String shapeName;
public Square(){
length = 8.0;
width = 8.0;
shapeName = "Square";
}
public double getArea(){
double Area;
Area = length * width;
return Area;
}
public double getPerimeter() {
double Perimeter;
Perimeter = (2*length) + (2*width);
return Perimeter;
}
public String getDescription() {
return shapeName;
}
}









share|improve this question


















  • 2




    You currently ask for user input a single time before you enter the loop, so it never changes afterwards. You have to move sc.nextLine() inside the loop, so the user is asked again each iteration.
    – QBrute
    Nov 11 at 0:13















up vote
2
down vote

favorite












So I've been working on this code for a while and it has me a bit lost. Keep in mind I am extremely new to Java and so I'm a bit slow on the uptake. I have created shape classes that implement an interface with getArea, getPerimeter and getDescription methods. There are multiple shapes but that isn't really where the problem is. The problem comes when I attempt to implement switch cases to allow the user to choose which shape he or she wants to add. I am getting the same message as many times as the Array of shapes will allow the address of the Shape I try to add. I realize the mistake I'm making is most likely a beginner one by I would really appreciate some help, Thank you. Also if you could give me a clue on how to sort the Shapes by their Area it would be greatly appreciated.



public class ShapeApp2 {

/**
* @param args the command line arguments
*/

public static void main(String args) {
Shape test = new Shape[10];
System.out.println("Choose a shape or type stop to break away?");
Scanner sc = new Scanner(System.in);
String Shape = sc.nextLine();

for (int i=0; i<test.length; i++) {

switch (Shape) {
case "Rectangle":
System.out.println("You have chosen a Rectangle");
test[i] = new Rectangle();
System.out.println("Enter another one now");
break;
case "Square":
System.out.println("You have chosen a Square");
test[i] = new Square();
System.out.println("Enter another one now");
break;
case "Equilateral Triangle":
System.out.println("You have chosen an Equilateral Triangle");
test[i] = new Equilateral_Triangle();
System.out.println("Enter another one now");
break;
case "Right Triangle":
System.out.println("You have chosen a Right Triangle");
test[i] = new Right_Triangle();
System.out.println("Enter another one now");
break;
case "Isosceles Triangle":
System.out.println("You have chosen an Isosceles Triangle");
test[i] = new Isosceles_Triangle();
System.out.println("Enter another one now");
break;
case "Scalene Triangle":
System.out.println("You have chosen a Scalene Triangle");
test[i] = new Scalene_Triangle();
System.out.println("Enter another one now");
break;
case "Stop":
break;
}
System.out.println(test[i]);
}

}


}



Also here's a couple of the Shape Classes for context.



package shapeapp2;

/**
*
* @author my-pc
*/
public class Rectangle implements Shape {
private double length;
private double width;
private String shapeName;
public Rectangle(){
length = 4.0;
width = 5.0;
shapeName = "Rectangle";
}
public double getArea(){
double Area;
Area = length * width;
return Area;
}
public double getPerimeter() {
double Perimeter;
Perimeter = (2*length) + (2*width);
return Perimeter;
}
public String getDescription() {
return shapeName;
}
}



package shapeapp2;

/**
*
* @author my-pc
*/
public class Square implements Shape {
private double length;
private double width;
private String shapeName;
public Square(){
length = 8.0;
width = 8.0;
shapeName = "Square";
}
public double getArea(){
double Area;
Area = length * width;
return Area;
}
public double getPerimeter() {
double Perimeter;
Perimeter = (2*length) + (2*width);
return Perimeter;
}
public String getDescription() {
return shapeName;
}
}









share|improve this question


















  • 2




    You currently ask for user input a single time before you enter the loop, so it never changes afterwards. You have to move sc.nextLine() inside the loop, so the user is asked again each iteration.
    – QBrute
    Nov 11 at 0:13













up vote
2
down vote

favorite









up vote
2
down vote

favorite











So I've been working on this code for a while and it has me a bit lost. Keep in mind I am extremely new to Java and so I'm a bit slow on the uptake. I have created shape classes that implement an interface with getArea, getPerimeter and getDescription methods. There are multiple shapes but that isn't really where the problem is. The problem comes when I attempt to implement switch cases to allow the user to choose which shape he or she wants to add. I am getting the same message as many times as the Array of shapes will allow the address of the Shape I try to add. I realize the mistake I'm making is most likely a beginner one by I would really appreciate some help, Thank you. Also if you could give me a clue on how to sort the Shapes by their Area it would be greatly appreciated.



public class ShapeApp2 {

/**
* @param args the command line arguments
*/

public static void main(String args) {
Shape test = new Shape[10];
System.out.println("Choose a shape or type stop to break away?");
Scanner sc = new Scanner(System.in);
String Shape = sc.nextLine();

for (int i=0; i<test.length; i++) {

switch (Shape) {
case "Rectangle":
System.out.println("You have chosen a Rectangle");
test[i] = new Rectangle();
System.out.println("Enter another one now");
break;
case "Square":
System.out.println("You have chosen a Square");
test[i] = new Square();
System.out.println("Enter another one now");
break;
case "Equilateral Triangle":
System.out.println("You have chosen an Equilateral Triangle");
test[i] = new Equilateral_Triangle();
System.out.println("Enter another one now");
break;
case "Right Triangle":
System.out.println("You have chosen a Right Triangle");
test[i] = new Right_Triangle();
System.out.println("Enter another one now");
break;
case "Isosceles Triangle":
System.out.println("You have chosen an Isosceles Triangle");
test[i] = new Isosceles_Triangle();
System.out.println("Enter another one now");
break;
case "Scalene Triangle":
System.out.println("You have chosen a Scalene Triangle");
test[i] = new Scalene_Triangle();
System.out.println("Enter another one now");
break;
case "Stop":
break;
}
System.out.println(test[i]);
}

}


}



Also here's a couple of the Shape Classes for context.



package shapeapp2;

/**
*
* @author my-pc
*/
public class Rectangle implements Shape {
private double length;
private double width;
private String shapeName;
public Rectangle(){
length = 4.0;
width = 5.0;
shapeName = "Rectangle";
}
public double getArea(){
double Area;
Area = length * width;
return Area;
}
public double getPerimeter() {
double Perimeter;
Perimeter = (2*length) + (2*width);
return Perimeter;
}
public String getDescription() {
return shapeName;
}
}



package shapeapp2;

/**
*
* @author my-pc
*/
public class Square implements Shape {
private double length;
private double width;
private String shapeName;
public Square(){
length = 8.0;
width = 8.0;
shapeName = "Square";
}
public double getArea(){
double Area;
Area = length * width;
return Area;
}
public double getPerimeter() {
double Perimeter;
Perimeter = (2*length) + (2*width);
return Perimeter;
}
public String getDescription() {
return shapeName;
}
}









share|improve this question













So I've been working on this code for a while and it has me a bit lost. Keep in mind I am extremely new to Java and so I'm a bit slow on the uptake. I have created shape classes that implement an interface with getArea, getPerimeter and getDescription methods. There are multiple shapes but that isn't really where the problem is. The problem comes when I attempt to implement switch cases to allow the user to choose which shape he or she wants to add. I am getting the same message as many times as the Array of shapes will allow the address of the Shape I try to add. I realize the mistake I'm making is most likely a beginner one by I would really appreciate some help, Thank you. Also if you could give me a clue on how to sort the Shapes by their Area it would be greatly appreciated.



public class ShapeApp2 {

/**
* @param args the command line arguments
*/

public static void main(String args) {
Shape test = new Shape[10];
System.out.println("Choose a shape or type stop to break away?");
Scanner sc = new Scanner(System.in);
String Shape = sc.nextLine();

for (int i=0; i<test.length; i++) {

switch (Shape) {
case "Rectangle":
System.out.println("You have chosen a Rectangle");
test[i] = new Rectangle();
System.out.println("Enter another one now");
break;
case "Square":
System.out.println("You have chosen a Square");
test[i] = new Square();
System.out.println("Enter another one now");
break;
case "Equilateral Triangle":
System.out.println("You have chosen an Equilateral Triangle");
test[i] = new Equilateral_Triangle();
System.out.println("Enter another one now");
break;
case "Right Triangle":
System.out.println("You have chosen a Right Triangle");
test[i] = new Right_Triangle();
System.out.println("Enter another one now");
break;
case "Isosceles Triangle":
System.out.println("You have chosen an Isosceles Triangle");
test[i] = new Isosceles_Triangle();
System.out.println("Enter another one now");
break;
case "Scalene Triangle":
System.out.println("You have chosen a Scalene Triangle");
test[i] = new Scalene_Triangle();
System.out.println("Enter another one now");
break;
case "Stop":
break;
}
System.out.println(test[i]);
}

}


}



Also here's a couple of the Shape Classes for context.



package shapeapp2;

/**
*
* @author my-pc
*/
public class Rectangle implements Shape {
private double length;
private double width;
private String shapeName;
public Rectangle(){
length = 4.0;
width = 5.0;
shapeName = "Rectangle";
}
public double getArea(){
double Area;
Area = length * width;
return Area;
}
public double getPerimeter() {
double Perimeter;
Perimeter = (2*length) + (2*width);
return Perimeter;
}
public String getDescription() {
return shapeName;
}
}



package shapeapp2;

/**
*
* @author my-pc
*/
public class Square implements Shape {
private double length;
private double width;
private String shapeName;
public Square(){
length = 8.0;
width = 8.0;
shapeName = "Square";
}
public double getArea(){
double Area;
Area = length * width;
return Area;
}
public double getPerimeter() {
double Perimeter;
Perimeter = (2*length) + (2*width);
return Perimeter;
}
public String getDescription() {
return shapeName;
}
}






java sorting switch-statement






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 0:11









C Jones

141




141








  • 2




    You currently ask for user input a single time before you enter the loop, so it never changes afterwards. You have to move sc.nextLine() inside the loop, so the user is asked again each iteration.
    – QBrute
    Nov 11 at 0:13














  • 2




    You currently ask for user input a single time before you enter the loop, so it never changes afterwards. You have to move sc.nextLine() inside the loop, so the user is asked again each iteration.
    – QBrute
    Nov 11 at 0:13








2




2




You currently ask for user input a single time before you enter the loop, so it never changes afterwards. You have to move sc.nextLine() inside the loop, so the user is asked again each iteration.
– QBrute
Nov 11 at 0:13




You currently ask for user input a single time before you enter the loop, so it never changes afterwards. You have to move sc.nextLine() inside the loop, so the user is asked again each iteration.
– QBrute
Nov 11 at 0:13












1 Answer
1






active

oldest

votes

















up vote
1
down vote













You are only reading the shape from the user once before the loop. You wanted to read it in the loop. That's the only thing that would make sense.



String Shape = sc.nextLine();
for (int i=0; i<test.length; i++) {


should be



for (int i=0; i<test.length; i++) {
String Shape = sc.nextLine();


Also, you should rename that variable. Shape looks like a classname.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244670%2fhow-can-i-correct-this-switch-case-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You are only reading the shape from the user once before the loop. You wanted to read it in the loop. That's the only thing that would make sense.



    String Shape = sc.nextLine();
    for (int i=0; i<test.length; i++) {


    should be



    for (int i=0; i<test.length; i++) {
    String Shape = sc.nextLine();


    Also, you should rename that variable. Shape looks like a classname.






    share|improve this answer

























      up vote
      1
      down vote













      You are only reading the shape from the user once before the loop. You wanted to read it in the loop. That's the only thing that would make sense.



      String Shape = sc.nextLine();
      for (int i=0; i<test.length; i++) {


      should be



      for (int i=0; i<test.length; i++) {
      String Shape = sc.nextLine();


      Also, you should rename that variable. Shape looks like a classname.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You are only reading the shape from the user once before the loop. You wanted to read it in the loop. That's the only thing that would make sense.



        String Shape = sc.nextLine();
        for (int i=0; i<test.length; i++) {


        should be



        for (int i=0; i<test.length; i++) {
        String Shape = sc.nextLine();


        Also, you should rename that variable. Shape looks like a classname.






        share|improve this answer












        You are only reading the shape from the user once before the loop. You wanted to read it in the loop. That's the only thing that would make sense.



        String Shape = sc.nextLine();
        for (int i=0; i<test.length; i++) {


        should be



        for (int i=0; i<test.length; i++) {
        String Shape = sc.nextLine();


        Also, you should rename that variable. Shape looks like a classname.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 0:13









        Elliott Frisch

        150k1388173




        150k1388173






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244670%2fhow-can-i-correct-this-switch-case-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma