Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. 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
  2.  
  3. orld!
  4.  
  5. Complete the following code:
  6.  
  7. import java.util.Scanner;
  8.  
  9. /**
  10. A program reads in a string, followed by an integer n,
  11. where the value of the integer should be smaller than the
  12. length of the string. The program then prints out the last
  13. n characters of the string.
  14. */
  15. public class Strings
  16. {
  17. public static void main (String[] args)
  18. {
  19. // Display prompt for string of characters
  20. System.out.println("Please enter a string: ");
  21.  
  22. // Read string
  23. Scanner in = new Scanner(System.in);
  24. String str = in.nextLine();
  25.  
  26. // Display prompt for integer n
  27. System.out.println("Please enter a value for n: ");
  28.  
  29. // Read n
  30. int n = in.nextInt();
  31.  
  32. // Print last n characters of string
  33.  
  34. // Your work here
  35.  
  36.  
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement