简体   繁体   中英

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 done so far. Currently, it's deleting only 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();

        } 

The format of your loop is wrong. You should use:

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();

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

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

What you had before was an empty do-while loop followed by one block of code, which was why it was only happening once. Note that I also changed the loop condition - we should loop while the length is greater than 140, not while it is less than.

For learning purposes, below is your original loop:

do {
    //you didn't do anything inside the loop!
} while (tweetLength <= 140);

//all of your code was after the loop

Edit:

We also needed to fix this line rand.nextInt(tweetLength) because that will return an int between 0 (inclusive) and tweetLength (exclusive). When this returns 0 the next line will break because you are calling substring(0, -1) . Thanks to PatrickParker for this point

You'd better replace the String by a StringBuilder , which is much faster for this kind of operations:

private static String shorten(String str, int length) {
    StringBuilder sb = new StringBuilder(str);
    Random rand = new Random();
    while (sb.length() > length) {
        int pos = rand.nextInt(sb.length());
        // The deleteCharAt() method deletes the char at the given
        // position, so we can directly use the retrieved value
        // from nextInt() as the argument to deleteCharAt().
        sb.deleteCharAt(pos);
    }
    return sb.toString();
}

To answer your question:

You are using a do-while loop, which has this format:

do {
    // Things to do
} while (condition);

Your block behind this piece of code has nothing to do with this loop. It's just an anonymous code block :

{
    // Statements
}

So first your empty do-while loop executes, and then the code below it — once, of course.

You should use a while loop instead:

while (lengthOfString > 140) {
    // remove a character
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM