Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Complete the program below so that it prompts for a string and an integer n, that is no larger than the length of the string, and then prints out the last n characters of that string. For example, given the input string, "Hello, World!" along with the integer 5, the output should be
- orld!
- Complete the following code:
- import java.util.Scanner;
- /**
- A program reads in a string, followed by an integer n,
- where the value of the integer should be smaller than the
- length of the string. The program then prints out the last
- n characters of the string.
- */
- public class Strings
- {
- public static void main (String[] args)
- {
- // Display prompt for string of characters
- System.out.println("Please enter a string: ");
- // Read string
- Scanner in = new Scanner(System.in);
- String str = in.nextLine();
- // Display prompt for integer n
- System.out.println("Please enter a value for n: ");
- // Read n
- int n = in.nextInt();
- // Print last n characters of string
- // Your work here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement