Вопрос

I have several strings in a file where I am supposed to stop and read the values from those strings. For example:

This is the first line
#1 stop = 300
This is the third line
This is the 4th line
#2 stop = 400
This is the 6th line

I need to stop at #1 and extract the value 300 from there. Then I have to stop at #2 and extract 400, and so on.

I am VERY new to Java and can't figure out what is wrong with my code. (I haven't gotten to extracting the values yet):

public static void main(String[] args) throws IOException { 
        //read
        File fromFile = new File("in.txt");         
        BufferedReader bufferedReader = new BufferedReader(new FileReader(fromFile));
        String line;
        String firstHandler="";
        while ((line = bufferedReader.readLine()) != null) { 
            bufferedReader.readLine();       
            if (firstHandler.startsWith("#1")){
                System.out.println(firstHandler);
                String[] parts = firstHandler.split("=");   
                System.out.println(Arrays.toString(parts));  
             } 
                 break;
        }

        System.out.println(line);
         bufferedReader.close();
    }
}

At this point it only prints the first line, which is not at all what I need. Can anyone explain to me how this should be done in the right way?

Это было полезно?

Решение 2

First of all, as pointed out in the comments, you need to match lines starting with a #, since there are multiple lines beginning with # but having a different second character.

Next, you need to check the value of the line that you are reading to check for the # character. So, you can get rid of the firstHandler variable and use the line variable instead.

Finally, you need to get rid of the break statement, since that causes the loop to exit after the first line itself. That is the reason you only see the first line on the screen.

Therefore, your code can be changed to something like this:

while ((line = bufferedReader.readLine()) != null) 
        {                
         if (line.startsWith("#"))
           {
            System.out.println(line);
            String[] parts = line.split("=");   
            System.out.println(Arrays.toString(parts));  
           } 
         }

Другие советы

The errors are in these 4 lines:

    String firstHandler="";
    while ((line = bufferedReader.readLine()) != null) { 
        bufferedReader.readLine();      
        if (firstHandler.startsWith("#1")){

You read one line from inside the while statement. And for each line read, you enter the block. But inside this block, you read yet another line.

And then, what you compare with "#1" is not the line that you have just read, but firstHandler, which is initialized as an empty string once, and never modified. The code should be:

    while ((line = bufferedReader.readLine()) != null) { 
        if (line.startsWith("#1")) {

The reader should also be closed in a finally block, but that's another matter.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top