You are not logged in. Please login at www.codechef.com 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

2★akash1357
485914
accept rate: 0%

closed 24 Nov, 18:18

vijju123's gravatar image

4★vijju123 ♦
11.3k1316

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


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

7
12 34 56 78 76 54 32

here is how to use StringTokenizer

import java.util.StringTokenizer;
.
.
.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
.
.
.
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: http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

link

answered 04 Aug '13, 11:56

tijoforyou's gravatar image

3★tijoforyou
4.2k52364
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

0★kokulraam
-91
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.

link

answered 04 Aug '13, 12:06

tijoforyou's gravatar image

3★tijoforyou
4.2k52364
accept rate: 15%

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

java.io.BufferedReader br=new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

int N=Integer.parseInt(br.readLine());
String input[]=(br.readLine()).split(" ");
int arr[]=new int[N];
for(int i=0;i<N;i++)
{
   arr[i]=Integer.parseInt(input[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)

link

answered 04 Aug '13, 19:38

codeanindya's gravatar image

1★codeanindya
855817
accept rate: 0%

edited 05 Aug '13, 17:16

tijoforyou's gravatar image

3★tijoforyou
4.2k52364

1

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(System.in);
    int[] arr= new int[5];
    int i=0;
       while(i!=5){
         arr[i]= ip.nextInt();
          i++;
       }
}
link

answered 22 Jun '16, 00:50

happy2496's gravatar image

2★happy2496
1
accept rate: 0%

-3

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. engalukum therium

link

answered 05 Aug '13, 15:49

kokulraam's gravatar image

0★kokulraam
-91
accept rate: 25%

What the hell? Is that exact copy of @tijoforyou's answer ?

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

Yes, it is apart from the last two words

(23 Aug '13, 22:00) sobhagya3★

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×1,064
×294

question asked: 04 Aug '13, 11:42

question was seen: 8,682 times

last updated: 24 Nov, 18:18