# Input in a two Dimensional Array.

Printable View

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• December 9th, 2011, 08:29
Mr.777
Input N number of times!!!
Can anyone give me idea of getting input 'N' number of times with a space difference between every input.
Forexample:
1 2 3 4 . . .

Note: We don't know the 'N' initially and i want to get input exactly 'N' number of times.

Kindly give me an idea.
Thanks
• December 9th, 2011, 11:12
Norm
Re: Input in a two Dimensional Array.
When do you find out the value of 'N'?
If the input record contains more than 'N' values, how will you know when you have enough and can ignore the rest of the values?
• December 11th, 2011, 23:00
Mr.777
Re: Input in a two Dimensional Array.
Quote:

Originally Posted by Norm
When do you find out the value of 'N'?
If the input record contains more than 'N' values, how will you know when you have enough and can ignore the rest of the values?

Let's assume, i will start my program and get a number from user say 'N'
And no, input record can not contain more than 'N' values. All values will be space separated. I've managed to do this by reading it in BufferedReader as a String and then after performing some operations, i however able to get this but i want efficient way of getting such input.
• December 12th, 2011, 06:19
Norm
Re: Input in a two Dimensional Array.
Quote:

i however able to get this but i want efficient way of getting such input.
Sorry, where is the problem. You ask the user for the value of N and then read in N values from the user. All the values would have to be separated to be recognized as separate values.
• December 12th, 2011, 06:23
Mr.777
Re: Input in a two Dimensional Array.
I don't want to read String everytime. What if i need to read integer values separated by a space each, 'N' times. I am looking for efficient method to read Integers separated by space 'N' times.
• December 12th, 2011, 06:27
Norm
Re: Input in a two Dimensional Array.
Quote:

I don't want to read String everytime. What if i need to read integer values
If you are reading from the keyboard, you can not read integer values. Everything typed in is a character.
How is the data entered for you to read? Is there a person at the keyboard that is entering the numbers?
• December 12th, 2011, 06:42
Mr.777
Re: Input in a two Dimensional Array.
Quote:

How is the data entered for you to read? Is there a person at the keyboard that is entering the numbers?
Yes, user will enter through Keyboard.
• December 12th, 2011, 06:48
Norm
Re: Input in a two Dimensional Array.
Then the program will be getting Strings.
• December 12th, 2011, 06:51
Mr.777
Re: Input in a two Dimensional Array.
Sorry Norm, i know i am not able to explain what i need. Let me try once more... uhh......huh
What i need is, the most efficient way of getting 'N' times input separated by a space between each input and place it in the array at the same time. I don't want to read in a String first and then after some operations, move it to the array. In short i want to get the most efficient way of doing this.
I think i am quiet clear now :) Well, if not, at least for me, i am quiet clear now :P
Thanks alot for your time Norm.
• December 12th, 2011, 06:55
Norm
Re: Input in a two Dimensional Array.
There are two parts to this:
Read the input from the user
Store the input into an array.

Quote:

I don't want to read in a String first and then after some operations, move it to the array.
That is the way it is done. Read a String, convert it to int, store it in the array.
• December 12th, 2011, 07:03
Mr.777
Re: Input in a two Dimensional Array.
Okay, may be there is nothing what i think of achieving in it. Thanks for the time Norm.
• December 12th, 2011, 21:00
Junky
Re: Input in a two Dimensional Array.
I'm still confused. How will the data be entered?
1[enter]2[enter]3[enter]
or
1 2 3[enter]

If user enters how many values will be entered 'N' then simply loop 'N' times for the first case. For the second case read entire String and split.
• December 12th, 2011, 23:08
Mr.777
Re: Input in a two Dimensional Array.
Quote:

If user enters how many values will be entered 'N' then simply loop 'N' times for the first case. For the second case read entire String and split.
1 2 3 (Enter)
This is how data will be entered.
And i am doing exactly like this but the problem is i want to know the most efficient way of getting input like this. Do you know any?
• December 13th, 2011, 06:19
Norm
Re: Input in a two Dimensional Array.
Quote:

the most efficient way of getting input like this.
Read the String, separate the words, convert the words to int, store the int in the next slot in the array.

What do you mean by "most efficient"?
Fewest lines of java code?
Fastest execution time?
• December 13th, 2011, 06:31
Mr.777
Re: Input in a two Dimensional Array.
Quote:

Originally Posted by Norm
Read the String, separate the words, convert the words to int, store the int in the next slot in the array.

What do you mean by "most efficient"?
Fewest lines of java code?
Fastest execution time?

Ofcourse Fastest execution time. I don't care for lines of code actually.
• December 13th, 2011, 06:39
Norm
Re: Input in a two Dimensional Array.
Have you found any other solutions than what have been posted so far?
• December 13th, 2011, 23:19
Mr.777
Re: Input in a two Dimensional Array.
Yes, and that is
Code :

```int val; int [] arr= new int[5]; int i=0; while((val=System.in.read())==' '){ arr[i]=val; i++; }```
But i am not sure if it's fast enough. Is it?
• December 14th, 2011, 05:38
Norm
Re: Input in a two Dimensional Array.
Add some code to time it.
What do you get in the array when you execute the code?
• December 14th, 2011, 06:05
Mr.777
Re: Input in a two Dimensional Array.
Quote:

What do you get in the array when you execute the code?
The problem is i can not get data inside array. Array still prints 0 as it's not even assigned any value. I know this will read character as it prints me ASCII but i somehow unable to manage to move it out of loop. It keeps getting input and doesn't move inside the loop as condition satisfies.
• December 14th, 2011, 06:08
Norm
Re: Input in a two Dimensional Array.
Read into a variable and print out the contents of the variable before using the data in the condition of a while loop. You need to see what read() returns.
• December 14th, 2011, 06:15
Mr.777
Re: Input in a two Dimensional Array.
It doesn't even get inside the loop :) I don't know why.
Also, if i do
Code :

```int val=0; try{ val=System.in.read(); }catch(Exception e){ System.out.err(e.getStackTrace()); } System.out.print(val);```
It prints the ASCII value which clearly means that it reads it as a character.
• December 14th, 2011, 06:25
Norm
Re: Input in a two Dimensional Array.
What value does the variable need to have to get into the loop?
• December 14th, 2011, 06:32
Mr.777
Re: Input in a two Dimensional Array.
Code :

```while((val=System.in.read())==' '){ //here }```
If i am not wrong, it's an empty character. Damn :@
Norm, thanks, i got it. I must get space ASCII :P How idiot i am :P
• December 14th, 2011, 06:38
Mr.777
Re: Input in a two Dimensional Array.
No, i ain't idiot :)
That didn't work too. I actually want it to get input as far as there is no Enter pressed. Well i managed to get it working.....
Code :

```int val=0; int arr[]=new int [5]; try{ int i=0; while((val=System.in.read())!='\n'){ arr[i]=val; i++; } } catch(Exception e){ System.out.println("N"); } for(int j=0;j<5;j++){ System.out.println(arr[j]); }```
But i know there are many problems with it for now as if i will enter 34 it will add ASCII's but Norm, do you think, this will execute more efficiently than InputStreamReader or BufferedReader?
• December 14th, 2011, 07:03
Norm
Re: Input in a two Dimensional Array.
For testing write a simple program with a loop that reads what you type in and prints it on the screen.
Quote:

will execute more efficiently than InputStreamReader or BufferedReader?
Write a test program that times how long each techniques takes.

You're wasting a lot of time worrying about efficiency when the program is dealing with a user.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last