-1

Ok so this is my code so far I really need help to make it quit after I type in quit and also for the program to not end after typing in one string. Any help or links to some help would be appreciated I am a very novice programmer.

import java.util.Scanner;
import java.io.*;

public class reverse 
{
    public static void main(String [] args)
    {
        String input = null;

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Please enter a string for reversal or type QUIT to exit: ");

        input = keyboard.nextLine();
        if(input.equals("quit"))
        {
           //close the program
        }

        Reverser(input, (input.length() - 1));
        System.exit(0);


    }
    public static void Reverser(String str, int size)
    {
        if(size>=0)
        {
            System.out.print(str.charAt(size));
            Reverser(str,size-1);
        }
    }




}
akay
  • 21
  • 6
  • 1
    move the `System.exit(0);` to the `if` condition. – Rajith Pemabandu May 28 '17 at 23:50
  • Dang that worked idk why i thought to leave it out – akay May 28 '17 at 23:54
  • 1
    If you want your program to be able to accept more than one string, you'd better learn about loops: `for` loops, `while` loops and `do...while` loops. Read about them [here](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html) and [here](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html), for starters. – Kevin Anderson May 28 '17 at 23:55

2 Answers2

0

You can try something like this

input = keyboard.nextLine();
while(!input.equals("quit"))
{
    Reverser(input, (input.length() - 1));
    System.out.println("Please enter a string for reversal or type QUIT to exit: ");
    input = keyboard.nextLine();
}
Devoto
  • 1
  • 1
0

Here is a possible solution, it doesn't end after typing multiple inputs:

    Scanner keyboard = new Scanner(System.in);

    String input;
    System.out.println("Please enter a String for reversal or type quit to exit:");
    while (!(input = keyboard.nextLine()).equals("quit")) { // wait until the input is quit
        // do stuff with input
    }

    keyboard.close(); // don't forget to close the scanner when you are done

Console output:

Please enter a String for reversal or type quit to exit:
blah
test
quit

Process finished with exit code 0

And if you are finished in the while loop, just add break;.