Reputation: 21
I want to create an array with for example integer values. In my Code below I create a while loop where I get an long string with much informations but now I need only 1 number value from this String, I get this number with the substring Method.
How is it possible to fill this substring into an array. Everytime in the while loop I get a new line from my file where are the Strings. And every time the number is different.
Here is my Code:
public static void main(String[] args) {
Text m;
String a;
Set<Integer> set = new HashSet<>();
try {
m = new Text();
while(m.hasLine) {
a = m.nextline(); // Here I get the string which looks like : <name id="654" time="123" number=”345” value=”789” />
String z2 = a.substring(10,13); // Here I get String : 654
String z3 = a.substring(21,24); // String: 123
String z4 = a.substring(35,38); // String: 345
String z5 = a.substring(48,51); // String: 789
String z6 = a.substring(60,64); // String: 4326
Integer i2 = Integer.valueOf(z2); //Convert all to integer values
Integer i3 = Integer.valueOf(z3);
Integer i4 = Integer.valueOf(z4);
Integer i5 = Integer.valueOf(z5);
Integer i6 = Integer.valueOf(z6);
//System.out.println(z2);
String r1;
if(set.contains(i2)){ // If The ID is in my array don't set it in the array only create string r1 with MEssage:2
r1 = "{\"Message\":2,";
}
else{
set.add(i2); // If the ID is not in my arrray set it in my array and create string r2 with Message: 1
r1 = "{\"Message\":1,";
}
// 1. How can I expand this array for the values of time; number; and value . So if the ID Value isn't in the array put it in there with the 4 values. and create String with Message: 2
// 2. And if the ID is in there check if value time is the same , if not , create String with Message 2
// 3. And if the ID is in there check if value time is not the same, then check if value number and value is the same ... if yes Create string Message 3: for example
// So how can I do such a thing. where I can create a 4 dimensional array and check if the values so in there ???
String s = r1;
System.out.println(s);
}
}catch (FileNotFoundException e) {
System.out.println("File not found!");
}
}
}
SO the main thing i want to do is in my while loop I get everytime the next line from my document. Line for Line!. Every Line I get as a String and in every String is an ID number. Now I want to create an array only with this id numbers. The reason why I want to do this, I have large document and it could be that one id is multiple times in there and in future I want for every line first to check if this id number is in my array and if it is there it should not fill this id in my array, because it is still there.
I hope you understand what I want to do !
Thank You very much !
Upvotes: 2
Views: 172
Reputation: 345
public static void main (String[] args) {
ArrayList<String> inputData = new ArrayList<String>();
try {
Scanner in = new Scanner(new BufferedReader(new FileReader("input.txt")));
while(in.hasNextLine())
inputData.add(in.nextLine());
Set<String> retVal = processData(inputData);
} catch (IOException e) {
System.out.println("IO error in input.txt or output.txt");
}
}
public static Set<String> processData(ArrayList<String> array) {
Set<String> set = new HashSet<String>();
List<String> nums = new ArrayList<String>();
for(String str : array){
String sa = str.substring(10, 13);
nums.add(sa);
set.addAll(nums);
}
return set;
}
This should be working, retVal will give you all unique numbers from string.
Upvotes: 0
Reputation: 245
This code converts the substring from 10 to 13 into an integer.
char one = z2.charAt(10);
char two = z2.charAt(11)
char three = z2.charAt(12)
char four = z2.charAt(13);
int num = (((int)one - 48)*1000) + (((int)two - 48)*100) + (((int)three - 48)*10) + ((int)one - 48);
Upvotes: 0
Reputation: 60046
Like @XtremeBaumer suggest in comment you can use a Set of Integers, so instead of String[] myArr = new String[10];
you can use :
Set<Integer> set = new HashSet<>();
Then you can fill the id in this Set
set.add(Integer.parseInt(z2));
This can solve your problem of duplication, and you can use any size of ids, not limit just 10
Upvotes: 1