View Single Post
Old 06-22-2009, 07:36 PM   #4 (permalink)
jamiemac2005
Design Team Member
 
jamiemac2005's Avatar
 
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,887
OS: Vista, various linux distros


Re: Java BufferedReader To get multiple user inputs

Dead Thread.

But just for the sake of it:
Code:
import java.io.*;

public class UserInputFromOneLine{
	public static void main(String[] args)**
		//output a menu, message, whatever
		System.out.println("Menu or whatever");
		//Output a message which suggests multiple input necessary
		System.out.print("Which choices would you like to select?: ");
		
		//Create the BufferedReader object...
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		//create a string to hold the user's input
		String choices = null;
		
		//get the user's input
		try{
			choices = br.readLine();
			//catch errors(vague method)
		} catch (IOException exc)**
			System.out.println("Error: Something went wrong...");
		}
		
		//set up a string to hold the inputs as an array:
		String[] choicesSplit = null;
		choicesSplit = choices.split(" ");
		//the array choicesSplit now contains the split choices... etc.
		
		//output a message(or post-process, just whatever you need to do)
		System.out.print("You chose: ");
		//output the choices
		for(String choice: choicesSplit)**
			//output the choice
			System.out.print(choice);
		}
		
	}
}
Cheers,
Jamey
__________________

Myspace
jamiemac2005 is offline   Reply With Quote