First, launch NetBeans and close any previous projects that may be open (at the
ID: 3705483 • Letter: F
Question
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "Parser" (without the quotation marks) that uses methods to:
Get a comma-delimited String of integers (e.g. "4, 8, 16, 32") from the user at the command line and then convert the String to an ArrayList of integers (using the wrapper class) with each element containing one of the input integers in sequence
Print the integers to the command line, using a for loop, so that each integer is on a separate line
Explanation / Answer
Hi.. I have written java program for the above.
Parser.java
import java.util.ArrayList;
public class Parser {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> ls = new ArrayList<Integer>();
if(args.length>0){
String num=args[0];
if(num.indexOf(",")>-1){
String ab[] = num.split(",");
for(String s:ab){
ls.add(Integer.parseInt(s));
}
}else{
ls.add(Integer.parseInt(num));
}
System.out.println("Values in arraylist are:");
for(Integer i:ls){
System.out.println(i);
}
}
}
}
Command line arguments:
4,6,8,22,24
Output:
Values in arraylist are:
4
6
8
22
24
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.