Results 1 to 14 of 14
Like Tree3Likes
  • 2 Post By sjicho
  • 1 Post By traisjames

Thread: Take multiple inputs in a single line.

  1. #1
    kunalgaurav18 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Take multiple inputs in a single line.

    Have a look at String.split().

  3. #3
    kunalgaurav18 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by Toll View Post
    Have a look at String.split().
    Will you please make it some clear with the help of some codes it is not becoming clear for me from your given link.

  4. #4
    sjicho is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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] + " ");
    }
    }
    }

  5. #5
    kunalgaurav18 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by sjicho View Post
    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] + " ");
    }
    }
    }
    Thanks!! It works

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by sjicho View Post
    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] + " ");
    }
    }
    }
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    sjicho is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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
    DarrylBurke and Fubarable like this.

  8. #8
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

    Default 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 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
    So what happens in the example given is:
    Java Code:
    String mystring;
    String[] myarray = new String[3];
    
    mystring = "boo:and:foo";
    myarray = mystring.split(':');
    which could also be
    Java Code:
    String mystring;
    int[] myarray = new int[3];
    
    mystring = "50 20 3";
    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.
    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.
    kunalgaurav18 likes this.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by traisjames View Post
    ...
    which could also be
    Java Code:
    String mystring;
    int[] myarray = new int[3];
    
    mystring = "50 20 3";
    myarray = mystring.split(' ');
    No, it couldn't also be.

    I suggest you test any code before posting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by DarrylBurke View Post
    No, it couldn't also be.

    I suggest you test any code before posting.

    db
    your right it should be

    Java Code:
    String[] myarray = new String[3];
    String mystring;
    mystring = "50 20 3";
    myarray = mystring.split(" ");
    I wrote it quick as an example.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by traisjames View Post
    your right it should be
    Java Code:
    String[] myarray = new String[3];
    No, it shouldn't be. Read the requirement:
    Quote Originally Posted by kunalgaurav18 View Post
    The first line is the number of test cases ...
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

    Default Re: Take multiple inputs in a single line.

    I know. again it was a demo to show the spit function

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by traisjames View Post
    it was a demo to show the spit function
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

    Default Re: Take multiple inputs in a single line.

    I did which is what I mentioned in my first post. I think they could be a little bit clearer...maybe runable examples. sometimes they can be confusing to read.

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2012, 01:11 AM
  2. Input class can't handle multiple inputs
    By musdem in forum New To Java
    Replies: 2
    Last Post: 03-17-2012, 12:03 AM
  3. Single Line Comments in JavaCC
    By mjdousti in forum Advanced Java
    Replies: 8
    Last Post: 06-05-2011, 08:36 PM
  4. Take two inputs in single line
    By Himanshu23 in forum New To Java
    Replies: 5
    Last Post: 12-25-2010, 01:56 AM
  5. if statement with multiple inputs?
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 01-20-2009, 04:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •