Advertisement
advictoriam

Untitled

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