Solved

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

Posted on 2011-03-16
5
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

695 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