Hi, my name is Mat and I'm new to the Java language. I recently started a programming class that focuses on Java. Unfortunately, I seem to be stuck on one my of my assignments . The assignment seems fairly simple but I seem to be stuck on the string manipulation. Here is the assignment:
-----
I need to enter a first name and then a last name, then an age from to 10 to 99. I must manipulate the strings so that I only get the first letter of the first name and the last name. However, the age that will be entered must then be reversed.
For example, lets say I would enter. Mat Proulx 91
the result would be: MP19
---
Here's what I have so far:
public static void main(String[] args) { String firstName, lastName; int age; System.out.println("Please enter your first name."); Scanner keyboard = new Scanner (System.in); firstName = keyboard.nextLine (); System.out.println("Please enter your last name."); lastName = keyboard.nextLine (); System.out.println("Please enter your age."); age = keyboard.nextInt(); String sentence = firstName + lastName + age; int position = sentence. indexOf (firstName); System.out.println (sentence);
If someone could lead me in the right direction, it would be appreciated. Thank you!
I have solved this for those who are interested :
public class Main { public static void main(String[] args) { String firstName,lastName,age; Scanner keyboard = new Scanner (System.in); //firstName System.out.println("Please enter your first name: "); firstName = keyboard.nextLine(); firstName = firstName.substring(0,1); firstName = firstName.toUpperCase(); //lastName System.out.println("Please enter your last name: "); lastName = keyboard.nextLine(); lastName = lastName.substring(0,1); lastName = lastName.toUpperCase(); //Age System.out.println("Please enter your age: "); age = keyboard.nextLine(); System.out.println("Your ID is: " + (firstName + lastName) + new StringBuffer(age).reverse()); }
-Nemus