Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How to reiterate while loop containing scanner in order to restart code in java?

$
0
0

Basically the issue is that once the main method completes, it prompts to restart the code or not, except it terminates after prompting, no matter what I enter. Note that this is just the main class. How do I fix this?

public static void main(String[] args) {    Scanner scan = new Scanner(System.in);    while (true) {              Random rand = new Random();            System.out.print("\nEnter what to paste: ");            String input = scan.nextLine();            System.out.print("\nEnter typing speed (10 is reccommended) (Lower is faster, higher is slower): ");            int speed = scan.nextInt();            try{Thread.sleep(3000);} catch(InterruptedException e){}            for(int i = 0; i < input.length(); i++){                try{Thread.sleep(rand.nextInt(30 * speed));} catch(InterruptedException e){}                int delete = rand.nextInt(20 * speed);                if(delete == 5){                    int randomChar = rand.nextInt(10);                    switch(randomChar){                        case 1: convert('a'); break;                        case 2: convert('b'); break;                        case 3: convert('c'); break;                        case 4: convert('d'); break;                        case 5: convert('e'); break;                        case 6: convert('f'); break;                        case 7: convert('g'); break;                        case 8: convert('h'); break;                        case 9: convert('i'); break;                        default:convert('j'); break;                    }                    try{Thread.sleep(40 * speed);} catch(InterruptedException e){}                    doType(KeyEvent.VK_BACK_SPACE);                    try{Thread.sleep(20 * speed);} catch(InterruptedException e){}                }                if(input.charAt(i) == '\\') {                    if(input.charAt(i + 1) == 't') {                        convert('\t');                        i++;                    }                    else if(input.charAt(i + 1) == 'n') {                        convert('\n');                        i++;                    }                    else                        convert(input.charAt(i));                }                   else                    convert(input.charAt(i));            }        System.out.print("\nDo you want to paste again? (y/n): ");            String choice = scan.next();            if(!choice.equals("y"))                break;    }         scan.close();    }

I tried making the main method its own method, then calling the method within a while loop in the main method. All the same result.


Viewing all articles
Browse latest Browse all 12111

Trending Articles