Results 1 to 3 of 3
  1. #1
    JavaComplient is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default switch case with integers and strings...

    Hi, I'm new here on these forums, and fairly new to Java. In my programming class at University, we are asked for our assignment to:

    "Write a Java application that prompts the user for an integer. Once the integer is received, use a switch statement to create a String variable to contain the number received followed by the appropriate suffix: “st ”, “nd ”, “rd ”, or “th ”. Note that there is a single space character after each of the suffixes.

    For example, if the integer: 3 is entered, the string variable will contain: “3rd ”. If the integer: 97 is entered, the string variable will contain: “97th ”. The space character after the suffix has been added so that you can use the String variable in a sentence without having to worry about adding a trailing space character. Finally generate the following output: The value entered was: <integer value entered> Which has been converted to: “<String variable created>“ (Note the: “ “ )
    Once you have this program working, add the looping construct of your choice to continuously prompt the user to enter an integer for which you will generate the above output. To exit the program, the user must enter a
    negative number (you may want to indicate this in a prompt somewhere).
    Remember to test your program. Your program must be able to handle all of the positive integer values between0 and 1,000."

    Sorry this is so long, but this is what i have so far:

    import java.util.Scanner;
    public class Assign5d
    {
    public static void main (String [] args)
    {
    Scanner keyboard = new Scanner(System.in);
    String UserNumber;
    int input, stringLength, lastNumberIndex;
    System.out.println("Please enter a whole integer: ");
    input = keyboard.nextInt();
    UserNumber = "" + input;
    stringLength = UserNumber.length(); //trying to get length of String to maybe help get the last number
    of the string with the number in it
    lastNumberIndex = UserNumber.lastIndexOf(input); //another method to try and get the last index
    value
    System.out.println(stringLength + " " + lastNumberIndex); //for testing purposes

    }
    }

    I really dont know where to go from here...but I would appreciate if someone could just give me a push in the right direction (not the full answer) so that I can learn. no one in my class knows what they are doing and i quite possibly have the worst programming teacher ever. Thanks for any help!

  2. #2
    JavaComplient is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    ok so i figured out the part to get the last character, using charAt (stringLength - 1) but now I do not know how to get the switch case working....I feel im close tho... this is what I have:

    import java.util.Scanner;
    public class Assign5d
    {
    public static void main (String [] args)
    {
    Scanner keyboard = new Scanner(System.in);
    String UserNumber;
    int input, stringLength, lastNumberIndex;
    char aChar;
    System.out.println("Please enter a whole integer: ");
    input = keyboard.nextInt();
    UserNumber = "" + input;
    stringLength = UserNumber.length();
    aChar = UserNumber.charAt(stringLength - 1);
    lastNumberIndex = UserNumber.lastIndexOf(input);
    System.out.println(stringLength + " " + aChar);
    switch (aChar)
    {
    case 0:
    System.out.println("Which has been converted to: " + aChar + "th"); // for when the last char is 0 (at least this is
    // what im going for)
    break;
    }
    }
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What happens when you run that for various values of input?

    The general syntax of the switch statement is described, for example, in Oracle's Tutorial. What happens when you add more cases?

Similar Threads

  1. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  2. Finding Strings, booleans and Integers
    By Pez in forum SWT / JFace
    Replies: 1
    Last Post: 07-19-2009, 02:24 PM
  3. [SOLVED] Making Switch Case Insensitive?
    By iPetey in forum New To Java
    Replies: 6
    Last Post: 04-09-2009, 04:46 PM
  4. Replies: 2
    Last Post: 02-23-2009, 12:34 AM
  5. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 11:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •