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;
}
}
java sorting switch-statement
add a comment |
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;
}
}
java sorting switch-statement
2
You currently ask for user input a single time before you enter the loop, so it never changes afterwards. You have to movesc.nextLine()
inside the loop, so the user is asked again each iteration.
– QBrute
Nov 11 at 0:13
add a comment |
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;
}
}
java sorting switch-statement
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
java sorting switch-statement
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 movesc.nextLine()
inside the loop, so the user is asked again each iteration.
– QBrute
Nov 11 at 0:13
add a comment |
2
You currently ask for user input a single time before you enter the loop, so it never changes afterwards. You have to movesc.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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 0:13
Elliott Frisch
150k1388173
150k1388173
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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