I have to write a program that repeats a sentence over a hundred times which also contains 8 random typos and have to have each sentence numbered.

the sentence is: I will never spam my friends again

the program's output should be

  1. I will never spam my friends again.
  2. I will never spam my friends again.
    etc.

    import java.util.Scanner;
    import java.lang.String;
    
    
    public class spam
    {
        public static void main(String[] args) 
        {
            int i;
            int k;
    
                for(i = 0; i < 100; i++)
                {
                    String spam = "I will never spam my friends again";
                    System.out.println(spam);
                }
            }
    
        }
    }
    

Now I know this isn't much, but what I would like to know is where I should go about. I'm not asking you to do my homework, but exactly how I should go about this, I haven't done java in several years, and I'm still a beginner with programming. I'm thinking that the string should be an array and then the array can be read through and then change some things around as the sentence repeats right? I guess that would be best, but it'd be hard because I'm horrible at arrays and I'm completely stumped as to how to number each iteration in increasing order

Edited 3 Years Ago by eldiablo1121: Was not formatted correctly

step one: set that String outside of your loop. there's no reason why you should create a new one each iteration.
two: create a method that takes a String and returns the same String, either with a random generated char added in some place, or a char removed from a random place. (in order to check that you don't print the same typo twice, store the lines with typos in it in a list or set where you only store unique values in, and you check the returnvalue of your method against the contents before choosing whether or not to return)

in your main method, keep a counter. from now and then you call that method and print the return value. when you do, add 1 to that counter.
if the counter is equal to eight, don't call that method again.

in your main method, keep a counter. from now and then you call that method and print the return value. when you do, add 1 to that counter

@stultuske:- why more counter is needed if we already have a for loop.We can use that value to display in your output instead of creating new.

All other steps are good.

IIM: that second counter (yes, you could also use the number of elements of the already created list) is needed since exact 8 (and only 8, not more, not less) lines should contain an error. you can not do that with just the counter for the loop, unless you plan to have those lines on fixed numbers, but that is not the case, they need to be 'at random'.
either you keep a counter, and have the little less loss of resources for keeping the counter, or you check the list each and every time you randomly want to add a typo whether or not you already added 8.
keeping a seperate counter seems more efficiƫnt to me, less times you have to check the nr of elements of the list.

This article has been dead for over six months. Start a new discussion instead.