• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing arrays of ints  RSS feed

 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a similar problem to this....
i'm supposed to enter 10 values and make them array elements and then display them with a space apart each...
it goes like this..

public static void main (String args[]) {
String inarray=JOptionPane.showInputDialog ("Enter number array: ");
int a=Integer.parseInt (inarray);
int []b={a};

for (int i=0; i<=b.length; i++){
System.out.print (b[i]+" ");
System.out.println ("");
}

}

but then it only prints the elements without spaces apart... and along with an error saying
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

help please...
Thanks a lot!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Please UseCodeTags when you post source code.

There are some things wrong with your code. First of all, how are you entering the number array? With spaces between the numbers, like "1 2 3"? If yes, then I'm surprised you don't get a NumberFormatException when you call Integer.parseInt.

Integer.parseInt parses the text you enter as a single number. Doing something like int []b={a}; will not make an array of numbers, if you entered numbers separated by spaces. You'll need to split the input string and parse each of the entered numbers separately.

You get an ArrayIndexOutOfBoundsException because the condition in your for-loop is wrong. It should be i < b.length, not i <= b.length.
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry... i was in a hurry a while ago...
i used JOptionPane to input the numbers... like 42330 without spaces...
then i parse it and make them elements of the array b...
it works..(well for me at least...)
but as for printing them with spaces... at first i didn't use the array.. i just used string and the substring... and it worked... but our instruction was to use arrays so i am currently having a hard time...

any suggestions are very very welcome... thank you very much!!!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case... it works, but it does not do what you think it does.

What happens is that you parse the input "42330" into a single number, that you store into variable a. Then you create an array, b. But this array contains only one element: the number 42330. Not five elements 4, 2, 3, 3, 0.

In principle, your loop is correct, if you use < instead of <= and if you remove the empty System.out.println("");.
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but isn't the System.out.println ("") just to have space between the last line of the program and the words Process complete???
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The System.out.println(""); is inside the loop, so it would be printed after every array element.
 
Peachy Manasis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after hearing your suggestions... my code became...



but it still doesn't print the numbers with spaces...
*sigh* wonder why...
please help..
thank you very much!!!
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are aware that Integer.parseInt() only returns a single int. So it will always print 1 int or throw an exception.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some useful troubleshooting tips that might help you understand what is going wrong (and better understand the point Wouter and Jesper make). These are useful things you can use in this situation, and in the future when your code is not doing what you are expecting. Try to do one, or both, of the following:

1. Change your loop as follows:

This will let you know where you are in the loop as things are printed out. It will shed some light onto what is happening. How many times are you looping? Is this the nubmer of times you are expecting.

2. Try changing the output so you place each array value in a single quote.

This way you can see for sure what the value of b[i] is. Is it what you are expecting?


These tips should help you see the issue.

Also carefully reread Jesper's post from Dec 4 @ 2:44PM. It explains what the problem is. Attention to detail is a critical skill to develop as a programmer.

>
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peachy Manasis wrote:but it still doesn't print the numbers with spaces...
*sigh* wonder why...
please help..
thank you very much!!!

Because the code is still wrong as I already explained you above. You are parsing the input as a single number, and then you create an array with one element. Not an array with 5 separate elements. A line of code like this: int[] b = {a}; does not magically split the digits in the number a into five separate numbers.

You will have to look at what you do with the input from the user. Delete these two lines:

You'll need to split inarray into separate numbers, and then create an array that contains the numbers as separate elements.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!