I have a project where I need to create a memory management program in Java and I have the text file but don't know where to begin. I'm already familiar with the split()
method in Java, but basically I want the index as the key and everything that comes after as a value.
Here's the text file cus it's big AF: https://pastebin.com/Q4whQHxW
Code:
String[] temp;
temp = data.split("\n"); //assume data is the string that contains the text
//doc contents
String[][] temp2 = new String[temp.length][];
for(int i = 0; i < temp.length; i++) {
temp2[i] = temp[i].split("( )");
}
for(int i = 0; i < temp2.length; i++) {
for(int j = 1; j < temp2[i].length; j++)
memoryPhysical.put(Integer.parseInt(temp2[i][0]),temp2[i][j]);
}
System.out.println(memoryPhysical);
You can use Map for key-value values. If you have any restrictions for using Map, let me know to write an array example. The code below shows you how to use Map:
import java.util.Arrays;
import java.util.HashMap;
public class App {
public static void main(String[] args) {
String text = "0 96\n" +
"1 29\n" +
"2 304\n" +
"3 561\n" +
"4 742\n" +
"5 621\n" +
"6 620\n" +
"11605 sub %14922, 12566\n" +
"11606 mov %15653, %12958";
HashMap<Integer, String> items = new HashMap<>();
Arrays.stream(text.split("\n")).forEach((s) -> {
int splitIndex = s.indexOf(" ");
String key = s.substring(0, splitIndex);
String value = s.substring(splitIndex);
items.put(Integer.parseInt(key), value);
});
System.out.println(items);
}
}
Output:
{0= 96, 1= 29, 2= 304, 3= 561, 4= 742, 5= 621, 11605= sub %14922, 12566, 6= 620, 11606= mov %15653, %12958}