Solved

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

Posted on 2011-03-16
5
355 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 53
difference between sorce folder and folder in eclipise 3 43
Print Rhino Java Array in Javascript 1 37
java mysql insert application 14 45
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 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