Results 1 to 3 of 3
- 10-21-2010, 04:29 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
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!
- 10-21-2010, 04:46 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
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;
}
}
}
- 10-21-2010, 06:52 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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
-
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 05:56 PM -
Finding Strings, booleans and Integers
By Pez in forum SWT / JFaceReplies: 1Last Post: 07-19-2009, 02:24 PM -
[SOLVED] Making Switch Case Insensitive?
By iPetey in forum New To JavaReplies: 6Last Post: 04-09-2009, 04:46 PM -
[SOLVED] Is it possible to have If in a switch case?
By sfe23 in forum New To JavaReplies: 2Last Post: 02-23-2009, 12:34 AM -
Switch Case and Key Events
By AndrewM16921 in forum New To JavaReplies: 4Last Post: 01-26-2009, 11:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks