You are not logged in. Please login at to post your questions!


[closed] Java Input

how to accept inputs on same line on console in java??? Plz help..

asked 04 Aug '13, 11:42

akash1357's gravatar image

accept rate: 0%

closed 24 Nov '17, 18:18

vijju123's gravatar image

4★vijju123 ♦

The question has been closed for the following reason "The question is answered, right answer was accepted" by vijju123 24 Nov '17, 18:18

12next »

Use StringTokenizer available in the java.util package.

For example, for the sample input given below:
First line contains N
Second line contains N integers

12 34 56 78 76 54 32

here is how to use StringTokenizer

import java.util.StringTokenizer;
BufferedReader br = new BufferedReader(new InputStreamReader(;
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; ++i) {
    arr[i] = Integer.parseInt(st.nextToken());

Fore more information on what a StringTokenizer is, or what it does or what all options are available when using a StringTokenizer, refer to the Javadoc here:


answered 04 Aug '13, 11:56

tijoforyou's gravatar image

accept rate: 15%

edited 04 Aug '13, 11:57

Answer is hidden because of too many downvotes. Click here to view.

answered 05 Aug '13, 15:47

kokulraam's gravatar image

accept rate: 25%

The idea is, you need to break down each of the separate items given on the single line.

The breaking down can be achieved by using delimiters (special characters, that are not part of the input themselves, but is used as a separator). The most common delimiter is the space character.

StringTokenizer is a class that does this task for you.


answered 04 Aug '13, 12:06

tijoforyou's gravatar image

accept rate: 15%

using StringTokenizer as answered by @tijoforyou is a good and smooth way. Another way of doing the same thing is :- . . . br=new;

int N=Integer.parseInt(br.readLine());
String input[]=(br.readLine()).split(" ");
int arr[]=new int[N];
for(int i=0;i<N;i++)


This will create a string array ('input') whose indices will hold every space separated input you gave. Afterwards we simply converted this string format to integer format. (this is almost similar to using StringTokenizer)


answered 04 Aug '13, 19:38

codeanindya's gravatar image

accept rate: 0%

edited 05 Aug '13, 17:16

tijoforyou's gravatar image



USACO recommends to use StringTokenizer, it's quicker...

(23 Aug '13, 21:58) betlista ♦♦3★

this code given below will do the same job. it doesn't matter whether you give input line by line or space separated it will accept input both ways.

import java.util.*;
 class test{
  public static void main(String[] args) {
    Scanner ip = new Scanner(;
    int[] arr= new int[5];
    int i=0;
         arr[i]= ip.nextInt();

answered 22 Jun '16, 00:50

happy2496's gravatar image

accept rate: 0%

I think the issue here is that every line of your information record is being ended via carriage return and line sustains characters, and the system is regarding each of these characters as a line eliminator - giving lines of information rotating with lines of vacant content. Essay Writing Company. In a perfect world, this kind of contrarily between document arrangements are Java info ought to never happen, however it has and you quite recently need to adapt.


answered 22 Jun '16, 11:18

myahbraiden's gravatar image

accept rate: 0%

Answer is hidden as author is suspended. Click here to view.

answered 07 Mar '17, 17:45

brandylehmann's gravatar image

accept rate: 0%

Answer is hidden as author is suspended. Click here to view.

answered 12 Apr '17, 10:45

william_riley's gravatar image

accept rate: 0%

Answer is hidden as author is suspended. Click here to view.

answered 12 Apr '17, 18:12

bethany34's gravatar image

accept rate: 0%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 04 Aug '13, 11:42

question was seen: 9,630 times

last updated: 24 Nov '17, 18:18