• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program won't return my array to main method  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, when this program asks the user to input 2 operands in the getOperand method, it never returns those operands to the array. Any idea why this is the case? On line 63 i declare op length to be two, so it should only have me input 2 integers, however keeps having me input forever, and never returns to the main method.

 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing out the value of i each time you pass through the loop.

for (int i = 0 ; i < op.length; ) {
    op[i] = input.nextInt();
}
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this? All this does is every number I type it outputs 0

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I mean to do this, but it still has me input operands forever
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:All this does is every number I type it outputs 0

Are you incrementing it anywhere?  Go back to the documentation and see what you are missing.
 
kennith stomps
Ranch Hand
Posts: 160
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was it, no increment for i, it's always something so simple.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The operands are now displayed in what I believe is hexadecimal format, do you guys know of any special technique for preventing this? I know it's caused by the array, but what can be done to prevent it.
Your operands are [I@55f96302
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the Arrays.toString(int[] a) method.  It will return a printable representation of the contents of the array.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so on line 59, I should replace
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you try and print the values of the array using this codeyou are seeing an output like: Your operands are [I@55f96302.


One way to print the actual values in the array would be to use the Arrays.toString(int[] a) method, which will give you a printable representation of the contents of the array:
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, that did it! Beautiful output
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We all know that the correct way to populate an array is with a loop, but I think we all know that wrong. It is sometimes better to use a Stream to populate an array, but I don't think this is one of those occasions. I think however, you can get a much easier array creation with an array initialiser:-That is easier to write for a two element array, but obviously not for big arrays. In the case of an array of reference types, an initialiser ensures that the size of the array exactly matches its elements, obviating the risk of a null which you don't know about.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!