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

×

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%


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%

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

4★tijoforyou
4.1k52364
accept rate: 14%

edited 04 Aug '13, 11:57

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

4★tijoforyou
4.1k52364
accept rate: 14%

ne enna avola periya appa takera

(05 Aug '13, 15:50) kokulraam0★

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

4★tijoforyou
4.1k52364

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%

Essays Tigers has always aimed to deliver high quality services and meeting the targets of students regardless of their level of urgency. However it has been our forte to never compromise on our quality and yet deliver remarkably feasible charges for our services. Our prices are pocket friendly and reasonable for any student within UK to avail without being bothered by their financial constraints.

link

answered 12 Apr, 10:45

william_riley's gravatar image

0★william_riley
1
accept rate: 0%

The above given code is very useful, but to truly reach your full potential you have do it a little differently than others. This is similar to the situation like when I write my essay or do my essay I try to be different and do it with full efficiency. This ensures that I get the best things.

link

answered 12 Apr, 18:12

bethany34's gravatar image

0★bethany34
1
accept rate: 0%

Here is the manner by which you would utilize the Scanner to process the same number of whole numbers as the client might want to inform Essay Help and put all esteem into a cluster. Be that as it may, you should just utilize this on the off chance that you don't know what number of whole numbers the client will include. On the off chance that you do know, you ought to just utilize Scanner.nextInt() the quantity of times you might want to get a number.

import java.util.Scanner; // imports class so we can use Scanner object

public class Test { public static void main( String[] args ) { Scanner keyboard = new Scanner( System.in ); System.out.print("Enter numbers: ");

    // This inputs the numbers and stores as one whole string value
    // (e.g. if user entered 1 2 3, input = "1 2 3").
    String input = keyboard.nextLine();

    // This splits up the string every at every space and stores these
    // values in an array called numbersStr. (e.g. if the input variable is 
    // "1 2 3", numbersStr would be {"1", "2", "3"} )
    String[] numbersStr = input.split(" ");

    // This makes an int[] array the same length as our string array
    // called numbers. This is how we will store each number as an integer 
    // instead of a string when we have the values.
    int[] numbers = new int[ numbersStr.length ];

    // Starts a for loop which iterates through the whole array of the
    // numbers as strings.
    for ( int i = 0; i < numbersStr.length; i++ )
    {
        // Turns every value in the numbersStr array into an integer 
        // and puts it into the numbers array.
        numbers[i] = Integer.parseInt( numbersStr[i] );
        // OPTIONAL: Prints out each value in the numbers array.
        System.out.print( numbers[i] + ", " );
    }
    System.out.println();
}

}

link

answered 11 Aug, 19:06

gill's gravatar image

0★gill
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★
toggle preview
Preview

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:

×977
×253

question asked: 04 Aug '13, 11:42

question was seen: 7,820 times

last updated: 11 Aug, 19:06