Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: java switch and if issue

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java switch and if issue

    thats what I'm trying to do:

    * If user inputs 3, onscreen message should say "A triangle has 3 sides."
    * If user inputs 4, onscreen message should say "A rectangle has 4 sides."
    * If user inputs 5, onscreen message should see "A pentagon has 5 sides."


    and here is my code:

    import java.util.Scanner;


    public class helloWorld{


    public static void main(String[] args) {

    String userInputStringOfAngles; // Declare a variable of type String to capture user input
    int numberOfAngles; // Declare a variable of type int to hold the converted user input

    Scanner myInputScannerInstance = new Scanner(System.in); // Recognize the keyboard
    System.out.print("Please type the integer press Enter: "); // Prompt the user
    userInputStringOfAngles= myInputScannerInstance.next(); // Capture user input as string
    numberOfAngles = Integer.parseInt(userInputStringOfAngles); // Convert the string to a number in case this will be useful later



    }
    int numberOfAngles=3;// LINE 1. CODE TO DETERMINE WHETHER USER INPUT IS OUT OF BOUNDS GOES HERE
    String userInputStringOfAngles;
    switch (numberOfAngles){
    case '3' : userInputStringOfAngles="3"
    break;
    case '4': userInputStringOfAngles="4"

    break;
    case '5':userInputStringOfAngles="5"

    break;
    default :
    System.out.println("Invalid input");

    // LINE 2. SWITCH CODE TO PRINT CORRECT "SHAPE" MESSAGE BASED ON USER INPUT GOES HERE


    }
    }
    if (userInputStringOfAngles==3){
    System.out.println("A triangle has 3 sides. ");


    };

    else if (userInputStringOfAngles==4){
    System.out.println("A rectangle has 4 sides");

    };
    else if (userInputStringOfAngles==5){
    System.out.println("A pentagon has 5 sides");
    }
    else System.out.println("Invalid input");
    }};

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java switch and if issue

    Do you have any specific java programming questions about your program?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: java switch and if issue

    You have many syntax errors. The first thing you need to do is to match up your braces {} and indent to clean up your code. That may help you figure out some of these errors.

    Regards,
    Jim

Similar Threads

  1. When to switch to Java 8?
    By GregBrannon in forum Java Theory & Questions
    Replies: 6
    Last Post: March 24th, 2014, 03:09 PM
  2. Im very new to java And switch statements
    By RevChe in forum Loops & Control Statements
    Replies: 9
    Last Post: March 17th, 2013, 10:14 AM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  5. Why Did You switch to Java?
    By Sisyphus in forum The Cafe
    Replies: 2
    Last Post: May 26th, 2010, 04:01 AM

Tags for this Thread