why can't I figure out array input
-
I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.
package biznes;
import java.io.InputStream;public class Vavejdane {
void Input() {
int[] client = new int[10];
for ( int i : client)
{
System.out.println("Vavedi masiva");
System.in.read(client[i]);
}}
}
-
I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.
package biznes;
import java.io.InputStream;public class Vavejdane {
void Input() {
int[] client = new int[10];
for ( int i : client)
{
System.out.println("Vavedi masiva");
System.in.read(client[i]);
}}
}
LORDA12 wrote:
for ( int i : client)
You are iterating over all integers stored in the array. Which means that i will take the values of the integers currently in the array (which are not initialized), which means i will have an undertermined value. The you use this i to access elements of your array, which is wrong. You should write your loop this way:
for ( int i=0; i
(not sure anymore if it is client.size or client.lenght, you have to look it up since I do not use arrays this way much often).
Cédric Moonen
Software developer
Charting control [v3.0]
OpenGL game tutorial in C++ -
LORDA12 wrote:
for ( int i : client)
You are iterating over all integers stored in the array. Which means that i will take the values of the integers currently in the array (which are not initialized), which means i will have an undertermined value. The you use this i to access elements of your array, which is wrong. You should write your loop this way:
for ( int i=0; i
(not sure anymore if it is client.size or client.lenght, you have to look it up since I do not use arrays this way much often).
Cédric Moonen
Software developer
Charting control [v3.0]
OpenGL game tutorial in C++ -
The problem is not there. Java initializes automatic to 0. The problem is in System.in.read(). I don't know what parameters I need to input the array from the keyboard.
LORDA12 wrote:
The problem is not there. Java initializes automatic to 0.
There's certainly a problem there anyway: first you shouldn't relly on initialization, this is a very habbit, and second even if everything is initialized at 0, it means that you will always store your character in the first element of your array (all the elements of your array are initially 0, so if you iterate over them and do client[i], it will always point to the first element of your array). Furthermore, did you look at the documentation of System.in.read ? It doesn't accept any parameter and returns a character. So, you have to write it this way:
client[i] = System.in.read();
Also, as it is returning a character, you should store the return values inside a character array. What do you want to do exactly ? Ask the user to type in numbers (so, he can enter several characters for one element of your entry) or ask the user to type in characters (one character for each element of your array) ? This makes a big difference.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.
package biznes;
import java.io.InputStream;public class Vavejdane {
void Input() {
int[] client = new int[10];
for ( int i : client)
{
System.out.println("Vavedi masiva");
System.in.read(client[i]);
}}
}
LORDA12 wrote:
I've read all articles in the internet
Really? all 24 billion of them? Wow, you should apply for an entry in the Guinness Book of Records. Did you look in the official Java documentation? the page on System.In? that explains how Read() works. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.
package biznes;
import java.io.InputStream;public class Vavejdane {
void Input() {
int[] client = new int[10];
for ( int i : client)
{
System.out.println("Vavedi masiva");
System.in.read(client[i]);
}}
}
Try this:
client[i] = System.in.read();
[editied so it'll work]
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H
modified on Monday, November 8, 2010 10:01 AM
-
I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.
package biznes;
import java.io.InputStream;public class Vavejdane {
void Input() {
int[] client = new int[10];
for ( int i : client)
{
System.out.println("Vavedi masiva");
System.in.read(client[i]);
}}
}