• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to display values in an array

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having a String[] array.

I need to display all its values separated by commas.

Like Iam having values like 1,2,3,4.

Vidhya
 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to loop through your array, and print them as you go or build a big long string, then print that when you're done.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use

System.out.println(Arrays.asList(theArray))

but that will enclose your list in [] marks.
 
Master Rancher
Posts: 3922
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Capture the output and substring out the data minus the []s
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do it in Java 5 way

use enhanced for loop
let string array you want to print is

A[]

then code will be


[edit]Corrected code tags. CR[/edit]
[ July 23, 2008: Message edited by: Campbell Ritchie ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shriya Kishore ,

Try this way :

String s[] = {new String("1"),new String("2"),new String("3"),new String("4"),new String("5")};
String str = Arrays.asList(s).toString();
System.out.println(str.substring(1, str.length()-1));
 
Sheriff
Posts: 22002
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of Arrays.asList(s).toString(), use Arrays.toString(s). These toString methods, added in Java 5.0, do the same thing without creating the List object.
 
Viidhya Kishore
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your valuable suggestions.
I stuck around wit Rob's and it is good now.

Thanks once again.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic