2

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 !

2
  • simply use a set. there you can only have unique values Commented May 17, 2017 at 10:40
  • Possible duplicate of Find the unique words in a text file Commented May 17, 2017 at 10:43

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

OK but i have to work with this array. if this id is in the array I want to create a new string with the Message number 1 or if this Id is not in this array i want to create a String with the number 2 and fill the id in this array. I edit some lines im my code , you can see it now. Do you understand what I want to do ?
@Meyer then you can use contains you can check if this number in your Set or not if(set.contains(number)){do this...}else{do this....}
@Meyer your Set contain numbers, so you have to convert z2 to an Integer so you can check with it and z2 should contain a number else it will throw an exception so if z2 contain only numbers you can use if(set.contains(Integer.parseInt(z2)){...}else{...})
I have one more question : Is it possible to create an HashSet with for example two Integer Values ? So that i can check if "integer 1" is still there check if the "integer 2" are still the same and so on ???
you can create a HashSet of an array for example Set<int[]> m = new HashSet<>(); maybe this is what you want @Meyer
|
0

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);

Comments

0
  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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.