Solved

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

Posted on 2011-03-16
5
351 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
Comment Utility
You can do just

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

Author Comment

by:Tolgar
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

12 Experts available now in Live!

Get 1:1 Help Now