Solved

Split a string using unix command line command in a Java code

Posted on 2011-03-16
5
352 Views
Last Modified: 2012-05-11

Say $gList is a list of 5 digit, 6 digit or 7 digit numbers and they are comma separated.

What does the following command return?

grep { /^\d+$/ } split /,/, $gList

Open in new window


I would like to run something like this within my Java code and run a for loop for each of these numbers?

How can do it?

Can you please send me a simple sample code?

Thanks,

0
Comment
Question by:Tolgar
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35151766
You can do just

String[] nums = list.split(",");
0
 

Author Comment

by:Tolgar
ID: 35151875
ok. but this returns string. And I already have string of numbers that are comma separated.

I would like to pass them into an array and loop through this array.

How can I do it?

Thanks,
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 35151899
>>ok. but this returns string.

No - it returns an array of strings
String[] nums = list.split(",");
for(String num : nums) {
   System.out.println(num);
}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 400 total points
ID: 35151907
then you just make
String[] nums = list.split(",");
int [] my_num = new int[nums.length];
for(int j=0; j<my_num.length; j++){
my_num[j]= Integer.parseInt(num[j]);
}

then my_num[j] will have your nt numbwers
0
 

Author Comment

by:Tolgar
ID: 35151984
Why did  you get the length twice? Shouldn't it be that way?

String[] nums = list.split(",");
int [] my_num = new int[nums.length];
for(int j=0; j<my_num; j++){
my_num[j]= Integer.parseInt(num[j]);
} 

Open in new window



Thanks,
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now