×

# [closed] Java Input

 0 how to accept inputs on same line on console in java??? Plz help.. asked 04 Aug '13, 11:42 48●5●9●14 accept rate: 0% 12.4k●1●4●21

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

 3 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 answered 04 Aug '13, 11:56 4.2k●5●23●64 accept rate: 15%
 1 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 4.2k●5●23●64 accept rate: 15%
 1 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
 0 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++; } }  answered 22 Jun '16, 00:50 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 answered 05 Aug '13, 15:49 -91 accept rate: 25% What the hell? Is that exact copy of @tijoforyou's answer ? (23 Aug '13, 21:57) 1 Yes, it is apart from the last two words (23 Aug '13, 22:00) sobhagya3★

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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,091
×300

question asked: 04 Aug '13, 11:42

question was seen: 9,304 times

last updated: 24 Nov '17, 18:18