Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Taking multiple inputs in single line separated by space in java?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Taking multiple inputs in single line separated by space in java?

    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.
    Last edited by Deep_4; November 7th, 2012 at 09:32 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Take multiple inputs in a single line.

    We're not a homework service so please don't take the mickey.
    Work on it yourself and come back with code and a specific problem you're facing. The simple answer however is to use the Scanner class with the String.split() method to get the inputs.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Take multiple inputs in a single line.

    Are you reading from standard input or from a file?

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by mia_tech View Post
    Are you reading from standard input or from a file?
    No the input is not read from a file. It is being given by the user in the same way i shown above.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Take multiple inputs in a single line.

    If you need help finishing the project post the code you have so far and describe the problem you have.

    If you don't know where to start, any search engine is a good place. Look for scanner and string classes for starters.

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Take multiple inputs in a single line.

    Quote Originally Posted by kunalgaurav18 View Post
    No the input is not read from a file. It is being given by the user in the same way i shown above.
    since your input is in the form of

    3
    10 20 30

    you could read the first line and then loop through the second line...

    /**code removed by moderator*/
    Last edited by jps; October 28th, 2012 at 01:14 AM. Reason: spoonfeeding

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Take multiple inputs in a single line.

    @mia_tech
    please read the problem with spoonfeeding.

Similar Threads

  1. Multiple cron expressions execution for a single cron trigger
    By jeevana in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2012, 02:46 PM
  2. Extracting a single line from a HTML
    By HomeAutomation in forum Other Programming Languages
    Replies: 1
    Last Post: May 23rd, 2012, 07:00 PM
  3. Best way to turn a single class into multiple classes
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2012, 10:57 AM
  4. Can single MDB subscribe multiple topics?
    By shptlucky in forum Web Frameworks
    Replies: 0
    Last Post: April 22nd, 2011, 05:52 AM
  5. Conversion from multiple ints to single string
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 09:08 PM

Tags for this Thread