loop/array? problem

My code works but I want to make an addition to it and i don't know how. Instead of just getting one line from the user and outputting that line I want to get multiple lines from the user (at once) until the user types 0 and then output them all (at once) excluding 0. Here is my program:

import java.io.*;

import java.util.*;

public class TrivialApplication {

public static void main(String args[]) throws IOException {

BufferedReader kinput =

new BufferedReader (new InputStreamReader (System.in));

String user_input = "starting string";

System.out.println("This program (called reversing) will take your input,"); System.out.println("reverse it, and then output it.");

while (!user_input.equalsIgnoreCase("q")) {

System.out.println("\n\nMenu\n");

System.out.println(" Continue: C");

System.out.println(" Quit: Q\n");

System.out.print("Please enter your choice from the menu: ");

user_input = kinput.readLine();

if((!user_input.equalsIgnoreCase("c")) &&

(!user_input.equalsIgnoreCase("q"))) {

System.out.println("That choice isn't available");

}

if (user_input.equalsIgnoreCase("c")) {

System.out.println("Please enter a series of strings which you want " + "to be reversed:\n");

user_input = kinput.readLine();

System.out.println("\nHere is a list of your strings reversed:\n");

if (!user_input.equals("0")) {

StringTokenizer myTokenizer = new StringTokenizer(user_input);

String[] new_string = new String[myTokenizer.countTokens()];

for (int c = 0;myTokenizer.hasMoreTokens(); c++) {

new_string[c] = myTokenizer.nextToken();

StringBuffer new_buffer = new StringBuffer(new_string[c]); new_string[c] = new_buffer.reverse().toString();

System.out.print(new_string[c] + " ");

}

}

if (user_input.equals("0")) {

}

}

}

}

}

An example of what i want to do is the user would input this:

hey how are you

moo cow

whats up

0

and the output would look like this:

yeh woh era uoy

oom woc

stahw pu

Thanks to all in advance!

[2211 byte] By [RAWR-itsanONIONa] at [2007-11-27 3:24:28]
# 1

Hi,

Your code has one while loop, and needs a second one.

When the user enters "C", the code needs to go into a while loop, which can only be broken out of when the user enters a "0".

The new while loop should look like this:

while (the user didn't enter zero)

{

store the string in an array

get the next string

}

When the user enters a "0", the while loop ends, and you then enter a for loop:

for (....)

{

Get the next string from the array

Reverse it and print it

}

After you print out the reversed lines, you need to clear out the string array so that the user can start again.

Below is your code with these changes.

The maximum size of the array is 200.

import java.io.*;

import java.util.*;

public class TrivialApplication

{

public final static int array_size = 200;

public static String [] stringArray = new String[array_size];

public static int counter = 0;

public static void main(String [] args) throws IOException

{

BufferedReader kinput = new BufferedReader( new InputStreamReader(System.in) );

String user_input = "starting string";

System.out.println("\n\nThis program (called reversing) will take your input,");

System.out.println("reverse it, and then output it.");

System.out.println("-");

while ( !user_input.equalsIgnoreCase("q") )

{

System.out.println("\n\n\n\n Menu\n");

System.out.println(" Continue: C");

System.out.println(" Quit: Q\n");

System.out.print("Please enter your choice from the menu: ");

user_input = kinput.readLine();

if ( (!user_input.equalsIgnoreCase("c")) &&

(!user_input.equalsIgnoreCase("q")) )

{

System.out.println("That choice isn't available");

}

if (user_input.equalsIgnoreCase("c"))

{

System.out.println("\nPlease enter a series of strings (no more than " +

array_size +

") which you want to be reversed:\n");

user_input = kinput.readLine(); // read the first string

while ( (!user_input.equalsIgnoreCase("0") ) ) // check if user entered "0"

{

stringArray[counter] = user_input;// store the string in the array

counter++;// add 1 to the counter integer

if (counter == array_size) // check the limit

break;// stop the loop if past limit

user_input = kinput.readLine();// read the next string

}

System.out.println("-");

System.out.println("Here is a list of your strings reversed:\n");

if (counter > 0) // if at least one string was entered

{

for (int i = 0; i < counter; i++) // access each string

print_Reversed_String( stringArray[i] );// call the method

}

System.out.println("-");

stringArray = new String[array_size];// reset the string array

counter = 0;// reset the counter integer

}

} // end while loop

} // end main() method

public static void print_Reversed_String(String user_input_string)

{

StringTokenizer myTokenizer = new StringTokenizer(user_input_string);

String [] new_string = new String[myTokenizer.countTokens()];

for (int c = 0; myTokenizer.hasMoreTokens(); c++)

{

new_string[c] = myTokenizer.nextToken();

StringBuffer new_buffer = new StringBuffer(new_string[c]);

new_string[c] = new_buffer.reverse().toString();

System.out.print(new_string[c] + " ");

}

System.out.println("");

} // end method

} // end class

Java_Lavaa at 2007-7-12 8:27:14 > top of Java-index,Java Essentials,Java Programming...
# 2

You could always use an ArrayList:

public class TrivialApplication

{

static List<String> myStrings = new ArrayList<String>();

.............

.............

.............

if (user_input.equalsIgnoreCase("c"))

{

System.out.println("Please enter a series of strings which you want "

+ "to be reversed (type \'0\' to quit):\n");

do

{

user_input = kinput.readLine();

if (user_input.charAt(0) != '0')

{

myStrings.add(user_input);

}

}

while (user_input.charAt(0) != '0');

System.out.println("\nHere is a list of your strings reversed:\n");

if (!myStrings.isEmpty())

{

for (int i = 0; i < myStrings.size(); i++)

{

String myStr = myStrings.get(i);

for (int j = 0; j < myStr.length(); j++)

{

System.out.print(myStr.charAt(myStr.length() - j -1));

}

System.out.println();

}

}

.............

.............

.............

I also recommend that you take out most of this stuff from the main method and put it into other sub-methods.

Good luck!

/Pete

petes1234a at 2007-7-12 8:27:14 > top of Java-index,Java Essentials,Java Programming...
# 3
double post. http://forum.java.sun.com/thread.jspa?threadID=5167509&messageID=9641849#9641849
bheilersa at 2007-7-12 8:27:14 > top of Java-index,Java Essentials,Java Programming...
# 4
thanks alot for all your help guys, right now i do not have access to eclipse since my only working computer right now is my laptop but I will be able to implement these ideas later. Duke points will be given out to all who helped, thanks again all!
RAWR-itsanONIONa at 2007-7-12 8:27:14 > top of Java-index,Java Essentials,Java Programming...
# 5
> double post.> > http://forum.java.sun.com/thread.jspa?threadID=5167509> &messageID=9641849#9641849Dang, I thought all of this looked familiar. Why didn't you read the answers on your previous post?!?
petes1234a at 2007-7-12 8:27:14 > top of Java-index,Java Essentials,Java Programming...
# 6
Sorry, i forgot i already posted this before, thanks for the answer and reminding me as well as giving me a link. DUKE STAR TO YOU!
RAWR-itsanONIONa at 2007-7-12 8:27:14 > top of Java-index,Java Essentials,Java Programming...