Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tweetshortener;
- // Imports for user input and random number generator
- import java.util.Random;
- import java.util.Scanner;
- /**
- *
- * @author mike
- */
- public class TweetShortener {
- public static void main(String[] args) {
- // Setup user input and string variable
- String tweet = new String();
- Scanner userInput = new Scanner(System.in);
- // Keep prompting for tweet if string is empty
- while (tweet.isEmpty())
- {
- // Prompt for tweet and take user input
- System.out.println("Please enter tweet: ");
- tweet = userInput.nextLine();
- // If tweet is under limit, post
- if(tweet.length() <= 140 && !tweet.isEmpty())
- {
- System.out.println("Tweet posted!");
- }
- // If tweet is over the limit, send it to the shortener
- else if (tweet.length() > 140)
- {
- String shortenedTweet = new String();
- shortenedTweet = shortenTweet(tweet);
- System.out.println("Tweet shortened and posted: \n" + shortenedTweet);
- }
- }
- }
- // Tweet shortener
- public static String shortenTweet(String tweet)
- {
- // Set up variables
- String shortenedTweet = new String();
- Random generator = new Random();
- int x;
- // While tweet is over the limit, remove characters at random
- while(tweet.length() > 140)
- {
- x = generator.nextInt(tweet.length()-1);
- // Create a new string around the randomly chosen deletion
- tweet = tweet.substring(0,x) + tweet.substring(x+1);
- }
- // Set and return shortened tweet
- shortenedTweet = tweet;
- return shortenedTweet;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement