• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about array  RSS feed

 
ben istaharov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I read an array type?

I want it not to be a random numbers.
The task is to get few numbers and then print it only if it 1 or 0;

can't do it...


 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I understand the question. You have an array, you populate it with 5 numbers, then you print M[i].
M is a poor name for an array, and it should not use a capital letter.
So what goes wrong?

When writing a for loop, declare the loop index in the loop. Not before. You want it to go out of scope. Then use a different number as the array index when you print it out. I suspect that i will reach 5, and you can't have a no 5 element in that array.
 
ben istaharov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all i want is an array to get 5 numbers
and then show then out.

and I cant make that work.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben istaharov wrote:all i want is an array to get 5 numbers
and then show then out.

If you want to print all the numbers in the array then you either have to have a loop that iterates over the array (like the one you have that fills the array) and in the body of the for loop print out the element at that index or you can just print the value returned by passing your array to the java.util.Arrays.toString() method.

But this isn't what you appeared to be asking in your first post where you mentioned only printing out 1's and 0's.
 
ben istaharov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



herer is the code.

that is the porblem:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException

at Assignment2.main(Assignment2.java:16)



lets start again please : i want a code line the allow you to enter numbers to array.

what is wrong with this?


 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben istaharov wrote:what is wrong with this?

for (int i = 0 ; i < madrid.length; i++ )

madrid[i] = BasicIO.ReadInteger();


Assuming BasicIO.ReadInteger() returns an int or Integer there is nothing wrong with that code per se.
The error is complaining about an unhandled exception. I don't know the BasicIO class but I would guess its ReadInteger method is decalred to throw an IOException, which is a checked exception, whch means you either have to put the call to ReadInteger in a try/catch statement or declare the main method to throw an IOException.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben istaharov wrote: . . . Exception in thread "main" java.lang.Error: Unresolved compilation problem: . . .
There would have been a red mark against that line before you tried to run the code. If Eclipse gives you a red mark, believe it. Put your mouse on it, and it may help you sort it out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!