0

I wrote a piece of code to read value from a file ,

public class Test { 
    public static void main(String[] args) throws Exception { 
    // pass the path to the file as a parameter 
        FileReader fr = new FileReader("/home/workspace_ag7_tmv/Message Router/environments/wb/conf/subscriber_content_restriction.conf"); 
        int i; 
        while ((i=fr.read()) != -1) {
            System.out.print((char) i); 
        } 
    }
}

In my file I passed these value : PRE|00000000110000200000049U POS|10000000110000200000049U

I am able to get these values using above piece of code, Now I want to fetch 4th index value. Can you please help me how to do same ?

5
  • 2
    what is 4th index mean here? Commented Oct 4, 2019 at 5:46
  • can you share , file screenshot then we can get idea about line sequence Commented Oct 4, 2019 at 5:49
  • PRE|00000000110000200000049U POS|10000000110000200000049U This is the file, I need to get value after PRE or POS i.e here, after PRE we have 0 value Commented Oct 4, 2019 at 5:59
  • 2
    If i recognize a pattern you want to eventually get the | position? or you want exactly the 4th char in index which may or may not be the | character? Commented Oct 4, 2019 at 6:02
  • I want the index of value after '|' position.Here it is 0 for PRE and 1 for POS Commented Oct 4, 2019 at 6:05

2 Answers 2

1

You could add the chars to an ArrayList and then get the element at the 4th index.

int i;
List<Character> charList = new ArrayList<>();    

while ((i=fr.read()) != -1) 
  charList.add((char) i);
  System.out.print((char) i); 
  }

// to get char at index 4
char a = charList.get(4);

After reading your other questions: If you want to get the index of the value that comes after '|' then you can convert the List to a String and get the indexOf '|' and add an 1 to that index.

Something like:

int i;
List<Character> charList = new ArrayList<>();    

while ((i=fr.read()) != -1) 
  charList.add((char) i);
  System.out.print((char) i); 
  }

      String str= charList.stream()             
                        .map(String::valueOf)   
                        .collect(Collectors.joining()); 
int firstPos = str.indexOf('|');
System.out.println(str.charAt(firstPos+1));

int secondPos = str.indexOf('|', firstPos+1);
System.out.println(str.charAt(secondPos+1));
Sign up to request clarification or add additional context in comments.

2 Comments

I tried but I am getting '?' while running above piece of code
try to archive what he is asking
0
/***********************************
    * a bit of ugly code               *
    * you read all lines in a list
    * you cycle the list and tokenizeit*
    * you get the PRE digit char in i  *
    * and the POS digit in i2          * 
    ***********************************/
    public class GetAllLines {
        StringTokenizer sToken;
        String s;
        public static void main(String[] args) {
            try {
                List<String> lines = Files.readAllLines(Paths.get("/home/workspace_ag7_tmv/Message Router/environments/wb/conf/subscriber_content_restriction.conf"));
                for (String line : Lines) {
                    sToken = new StringTokenizer(line, "|");
                    while (sToken.hasMoreElements()) {
                        //you can get after PRE 1st digit
                        s=sToken.nextElement().nextElement().toString();//the 1st getElementgets the PRE the 2nd gets all string up to POS
                        int i = Integer.parseInt(s.getSymbol(0).charAt(0)); //you get the next to PRE
                        s=sToken.nextElement().toString();//the 3rd getElement gets all string after POS
                        int i2 = Integer.parseInt(s.getSymbol(0).charAt(0)); //you get the next digit after POS
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

1 Comment

You could also stringtokenize with PRE and then POS too. Depends on what you want to do later in code with the line string. hope i gave you some help. You could even "for" the linestring upto 4 or 30 times empty and get the value there. many options on these.

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.