mardi 7 février 2017

Delete Random Characters From String

This is a project for school. The objective is to create a program that reads a user's input, then shortens that input by randomly deleting characters until it reaches 140 characters. Here's what I have so far. Currently it only deletes one character and then stops running. Thanks for any advice

import java.util.Scanner; import java.util.Random;

public class Main {

public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the tweet you want to shorten:");
        String tweet = null;

        tweet = keyboard.nextLine();

        int tweetLength = tweet.length();

        Random rand = new Random();


        do {

        } while (tweetLength <= 140); {
            int characterposition = rand.nextInt(tweetLength);
            String shorttweet = tweet.substring(0, characterposition-1);
            String shorttweet2 = tweet.substring(characterposition);

            tweet = shorttweet + shorttweet2;
            System.out.println("Shortented Tweet: " + tweet);
            tweetLength = tweet.length();

        } 




Aucun commentaire:

Enregistrer un commentaire