1

I'm trying to write a part of an String array into a String but I just stuck at a problem. The disposal of the value of line is always like this: "status.test.status.close.name = Closed". The only static of this value is "status." and ".name". I just want to get the part between "status." and ".name". With the code below I get this result: "status.test.status.close". My question now is, is it possible to delete parts of an array, for example: technicalNames.delete["status."];? Or does anyone has another hint how to realize it?

public void setTechnicalName(File javaFile) throws IOException {
    if(javaFile.exists()) {
        BufferedReader reader = new BufferedReader(new FileReader(javaFile));
        String line = null;
        while((line = reader.readLine()) != null)
        if (line.contains("In approval") || line.contains("In Approval") || line.contains("In review") || line.contains("In Review") || line.contains("Closed")){
                System.out.println(line);
                String[] technicalNames = line.split(".name");
                String technicalName = technicalNames[0];
                System.out.println(technicalName);

        }

        reader.close();
    }
}

That is the .xml file i read out:

status.test.status.close.name      = Closed
status.test.status.in.approval.name = In approval
status.test.status.in.review.name     = In review

test.field.approver1      = Approver
test.field.lookupworkflow = 
test.field.temp           = temp

Thanks in advance!

6
  • You can't delete parts of a string - you can only copy parts of a string into a new string. Please provide us with a input->output mapping of actual vs. desired value and your code so far to achieve this mapping. Commented Dec 1, 2014 at 7:36
  • You should probably use StringBuilder. It gives you more control of the strings & is easier to use Commented Dec 1, 2014 at 7:38
  • you can replace part of the string with an empty string result of which is equivalent to delete :) Commented Dec 1, 2014 at 7:39
  • @IlyaOvesnov not if you want to join again afterwards. You'd end up with double .. Commented Dec 1, 2014 at 7:41
  • @IlyaOvesnov technically speaking, Java Strings are immutable, so there's no "replacing" but "copying into a new String with parts omitted". Commented Dec 1, 2014 at 7:44

3 Answers 3

1

I am assuming that you are interested in parts between status and .name. You can try this way of doing it.

public  static void setTechnicalName(File javaFile) throws IOException {
    if(javaFile.exists()) {
        BufferedReader reader = new BufferedReader(new FileReader(javaFile));
        String line = null;
        int statusOffet = "status.".length();
        while((line = reader.readLine()) != null){
            int indexOfStatus = line.indexOf("status");
            int indexOfName = line.lastIndexOf(".name");                
            boolean isReqLine = line.contains("In approval")
                                        || line.contains("In Approval")
                                        || line.contains("In review")
                                        || line.contains("In Review") 
                                        || line.contains("Closed");

            if(isReqLine && indexOfStatus != -1 && indexOfName != -1){
                System.out.println(line);
                String stage = line.substring(indexOfStatus + statusOffet, indexOfName); 
                System.out.println(stage);
            }

        }
        reader.close();
    }
}

EDIT : as per comment to match format, I have included "." when calculating offset and used indexOf

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

1 Comment

Hey, that also includes a logic failure, because of the second status. The result I want to get is "test.status.close" but after running your code it returns only ".close" because the indexOfStatus is starting at the second status in the line. Is there a opportunity to let it start from the first status?
0

When you want to split your given line into parts, you can do it similar to

final String string = "status.test.status.close.name = Closed";
final String[] split = string.substring(0, string.indexOf("=")).split("\\.");
System.out.println("split = [" + split[0] + ", " + split[1] + ", " + split[2] + ", " + split[3] + ", " + split[4] + "]"); // split = [status, test, status, close, name ]

and pick the appropriate values out of split afterwards.

6 Comments

I would have done this before, but the problem is, that sometimes there are four words between status and name. That's why I need an dynamic solution for the problem. That is the .xml I read out: status.test.status.close.name = Closed status.test.status.in.approval.name = In approval status.test.status.in.review.name = In review
Then why don't you pick everything but the first and last item?
Because I never know how many items will be there. Is there a function to do something like split[lastItem]?
But what about split.length - 2 as last item?
As long as I don't know how many items are coming before split.length -2, I cannot use it. Otherwise there would be one item to much or to less.
|
0

Well, you have status defined twice. So assuming you are referring to the first status, and the desired result is to print out test.status.close you could do this if you knew that indexes 0 and n-1 would be not of interest:

StringBuilder builder = new StringBuilder();
String[] parts = line.split(".name")[0].split("\\.");
for(int i = 1; i < parts.length - 1; i++){
    builder.append(parts[i]);
    if(i < parts.length - 1){
    builder.append(".");
}
System.out.println(builder.toString())

3 Comments

I tried yours but I think there are some mistakes in your code? Shouldn't be parts an array? No matter if it is right or wrong, it doesn't seem to work, because parts is always empty.
Now it is working, but there are still some logic failures inside. Firstly it should be i < parts.length not i < parts.length -1, because otherwise i get "test.status." as the result. But even with i < parts.length the result is "test.status.close." so one dot to much at the end. And for lines with 4 words between "status." and ".name" it doesn't work at all. Any idea?
the additional . is the if statement. Define "doesnt work at all"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.