Results 1 to 14 of 14
- 10-26-2012, 09:05 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Take multiple inputs in a single line.
I want to take input in the following way:
Sample Input 1
3
45 3 14
Sample Input 2
5
12 34 5 56 7
The first line is the number of test cases and the second line is the corresponding values for the test cases.
Please give me the java code for taking multiple inputs in the single line separated by a space.
- 10-26-2012, 09:33 AM #2
Re: Take multiple inputs in a single line.
Have a look at String.split().
- 10-26-2012, 12:55 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
- 10-26-2012, 01:01 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Take multiple inputs in a single line.
try
import java.util.Scanner;
public class Random {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Number of integers: ");
int intNumberOfIntegers = scanner.nextInt();
int intStorage[] = new int[intNumberOfIntegers];
System.out.println("Please prompt numbers!");
for(int i = 0;i < intStorage.length;i++) {
intStorage[i] = scanner.nextInt();
}
System.out.println("Your numbers are");
for(int i = 0;i < intStorage.length;i++) {
System.out.print(intStorage[i] + " ");
}
}
}
- 10-26-2012, 01:16 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
- 10-26-2012, 01:44 PM #6
Re: Take multiple inputs in a single line.
1. Don't spoonfeed. With the guidance already given, the OP could have figured out the code -- or asked for clarification. A forum is a place to learn, not a free code handout center.
2. Go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.
3. It's bad practise (a) to name a class after an existing JDK class and (b) to use a meaningless class name that doesn't reflect its purpose.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-26-2012, 04:22 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Take multiple inputs in a single line.
Sorry for that it won't happen again I am also new to this forum but I do acknowledge my mistakes
- 10-26-2012, 06:47 PM #8
Member
- Join Date
- Oct 2012
- Location
- NE Iowa
- Posts
- 11
- Rep Power
- 0
Re: Take multiple inputs in a single line.
When you look at String.split(), one thing it says is the call [CODE]public String[] split(String regex)[/CODE
Other important infomation isSo what happens in the example given is:Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
The string "boo:and:foo", for example, yields the following results with these expressions:
Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
which could also beJava Code:String mystring; String[] myarray = new String[3]; mystring = "boo:and:foo"; myarray = mystring.split(':');
For both of these, lines 1 and 2 define the variables we are going to work with, line two is the array which the string will be seperated and stored into.Java Code:String mystring; int[] myarray = new int[3]; mystring = "50 20 3"; myarray = mystring.split(' ');
Line 3 is the data we are going to work with. This data can come in via some file or user entered data. : or a space is what seperates the parts of data.
Line 4 does the work of splitting the data based on the character listed in the (). The output is an array where each element is what is between the separator. This is what the Returns line in the description sheet says.
- 10-26-2012, 07:31 PM #9
- 10-26-2012, 07:47 PM #10
Member
- Join Date
- Oct 2012
- Location
- NE Iowa
- Posts
- 11
- Rep Power
- 0
- 10-26-2012, 09:14 PM #11
- 10-26-2012, 09:30 PM #12
Member
- Join Date
- Oct 2012
- Location
- NE Iowa
- Posts
- 11
- Rep Power
- 0
Re: Take multiple inputs in a single line.
I know. again it was a demo to show the spit function
- 10-27-2012, 05:21 AM #13
Re: Take multiple inputs in a single line.
Java has methods. Not functions.
You may think I'm being nitpick-ety, but written communication on a forum, unlike verbal communication whether face-to-face or via telecom, can't be instantly clarified when there's a slight misunderstanding. Because of that, it is necessary to be precise. Anything less could mislead the reader.
Have you seen the documentation for String#split(...)? There's an example provided there, which should be adequate for someone learning how to use the method.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-27-2012, 05:36 AM #14
Member
- Join Date
- Oct 2012
- Location
- NE Iowa
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
How to store multiple user inputs - should be a simple answer but I am lost.
By doowstados in forum New To JavaReplies: 1Last Post: 03-19-2012, 01:11 AM -
Input class can't handle multiple inputs
By musdem in forum New To JavaReplies: 2Last Post: 03-17-2012, 12:03 AM -
Single Line Comments in JavaCC
By mjdousti in forum Advanced JavaReplies: 8Last Post: 06-05-2011, 08:36 PM -
Take two inputs in single line
By Himanshu23 in forum New To JavaReplies: 5Last Post: 12-25-2010, 01:56 AM -
if statement with multiple inputs?
By soc86 in forum New To JavaReplies: 3Last Post: 01-20-2009, 04:44 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks