?
Solved

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

Posted on 2011-03-16
5
Medium Priority
?
364 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 400 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 1600 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

Industry Leaders: 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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

764 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