Skip to content

Commit 8706fc4

Browse files
committed
feat(ConvertNumbersIntoArrayList): add program to parse numbers from file into ArrayList
What - Added ConvertNumbersIntoArrayList in Package. - Reads a text file containing numbers separated by commas. - Uses StringTokenizer to split numbers and stores them in an ArrayList<Integer>. - Prints the resulting list to console. Why - Demonstrates file reading and parsing of delimited data. - Shows how to convert raw text input into structured Java collections. - Useful for lightweight parsing of CSV-style numeric data. How - Opened FileInputStream on a file path containing comma-separated numbers. - Read file contents into byte array, then converted to String. - Created StringTokenizer with `","` as delimiter. - Iterated over tokens, converted each String token to Integer using Integer.valueOf(). - Added integers into ArrayList. - Printed the ArrayList for verification. Logic - FileInputStream reads raw bytes from file. - new String(b) constructs a string representation of file contents. - StringTokenizer splits input based on specified delimiter (comma here). - Integer.valueOf(s) safely parses numeric tokens into Integer objects. - ArrayList grows dynamically as tokens are parsed. Real-life applications - Parsing CSV or configuration files where numbers are stored as comma-separated values. - Converting external input (from text files, network logs, etc.) into usable Java collections. - Preprocessing datasets for further computation or analytics. Summary - ConvertNumbersIntoArrayList reads a file of comma-separated numbers. - Splits data into tokens and converts each into Integer. - Stores parsed integers in ArrayList and prints the final collection. - Demonstrates integration of File I/O, String processing, and collection handling in Java. Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>
1 parent fb2cf1c commit 8706fc4

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

Section25CollectionFramework/src/StringTokenizerDemo/ConvertNumbersIntoArrayList.java renamed to Section23JavaIOStreams/String Tokenizer/src/Package/ConvertNumbersIntoArrayList.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package StringTokenizerDemo;
1+
package Package;
22

33
import java.io.FileInputStream;
44
import java.util.ArrayList;
55
import java.util.StringTokenizer;
66

77
public class ConvertNumbersIntoArrayList {
8-
public static void main(String[] args)throws Exception
9-
{
10-
FileInputStream fis = new FileInputStream("C://Users\\somes\\Downloads\\JAVA SE\\Section25CollectionFramework\\src\\StringTokenizerDemo\\Numbers.txt");
11-
8+
public static void main(String[] args)throws Exception {
9+
FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/String Tokenizer.txt/src/Package/Numbers.txt");
1210
byte b[]=new byte[fis.available()];
1311

1412
fis.read(b);
@@ -19,11 +17,10 @@ public static void main(String[] args)throws Exception
1917
String s;
2018
ArrayList<Integer> al = new ArrayList<>();
2119

22-
while (stk.hasMoreTokens())
23-
{
20+
while (stk.hasMoreTokens()) {
2421
s=stk.nextToken();
2522
al.add(Integer.valueOf(s));
2623
}
2724
System.out.println(al);
2825
}
29-
}
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10,20,30,40,50,60,70,80,90,100

0 commit comments

Comments
 (0)