• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing Array Elements in Java  RSS feed

 
Shivv Krishna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have tried to print array elements using standered print statement. I am getting errors. Please guide me how to print them.

Here is my code:
class arrayEx1{
public static void main(String args[])
{
int a[]=new int[3]; //Declaring Single Diomentional Array
a[0]=10;
a[1]=20;
a[2]=30;
int total=a[0]+a[1]+a[2];
System.out.println("Values stored in a[0],a[1],a[2]elements are :" + a[0] a[1] a[2]);
System.out.println("Total values of a[0],a[1],a[2]elements is :"+ total);
}
}

if i give comma (,) in between above print stament (print statement 1) stil i am getting errors.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't just string together stuff and have it print. Java is VERY specific (just like every other language) on how you combine things.

If you want to concatenate two strings, you need a '+' between them..Something like:



Note: this may or may not be what you want...but since you don't say, it's hard for anyone to know how to help you. You need to TellTheDetails, because ItDoesntWorkIsUseless.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you remove a[1] a[2] from the print statement, I bet it will compile. Then, add back one array element, then the other, making sure it will compile (hint: you may need more plus signs).
 
Shivv Krishna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Knute Snortum, fred rosenberger for your response.. it's started working with + symb in between array elements.. i missed a basic stuff...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivv Krishna wrote:i missed a basic stuff...

Everyone here has done this, or something similar. It's how you learn.

The good news is that this is one less mistake you will make in the future!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!