0

i am trying to read a file and print only the first number from each line . i have tried to use split, but it never return correct result , it just print entire contetn as below in the table. Any help would be highly appreciated

**thats my file** 

 40    3  Trottmann
 43    3  Brubpacher
252    3  Stalder
255    3  Leuch
258    3  Zeller
261    3  Reolon
264    3  Ehrismann
267    3  Wipf
270    3  Widmer 

**expected output** 
 40
 43
258
261
264
267
270

output

258
261
264
267
270

public class word {

            public static void main(String[] args) {
                
                // Create file
                File file = new File("/Users/lobsang/documents/start.txt");
    
                try {
                    // Create a buffered reader
                    // to read each line from a file.
                    BufferedReader in = new BufferedReader(new FileReader(file));
                    String s;
    
                    // Read each line from the file and echo it to the screen.
                    s = in.readLine();
                    while (s != null) {
                        
                          
                        
                        System.out.println(s.split("\s")[0]);
                    
                        
                        s = in.readLine();
                    }
                    
                    // Close the buffered reader
                    in.close();
    
                } catch (FileNotFoundException e1) {
                    // If this file does not exist
                    System.err.println("File not found: " + file);
    
                } catch (IOException e2) {
                    // Catch any other IO exceptions.
                    e2.printStackTrace();
                }
            }
    
        }
  • The Regex in your `split` method is technically correct, but you need to escape the backslash in java. So the fixed line would be: `System.out.println(s.split("\\s")[0]);` – maloomeister Nov 16 '20 at 15:00
  • Thanks it did worked .. can you please looks once again at my output .. made some changes.. it output just empty for all number which has digit less than three . may be because of initial space instead of number .. how can i update my expression so that it display all number including 1 and 2 digit numbe r – kunga dolma Nov 16 '20 at 15:35
  • I added an answer down below. – maloomeister Nov 16 '20 at 15:42

3 Answers3

1

You need to double backslash for the backslash to be read, so do this:

System.out.println(s.split("\\s")[0]);

instead of:

System.out.println(s.split("\s")[0]);

hallo545401
  • 115
  • 13
1

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash (\). Escape sequence for white space is \s so you need to replace "\s" with "\\s"

0

As I already answered in the comments, you need to escape the backslash in java. Furthermore, you can trim() the string before splitting it, which removes leading and trailing whitespaces. This means, it will also work with two digit or one digit numbers. So what you should use is:

System.out.println(s.trim().split("\\s")[0]);
maloomeister
  • 2,461
  • 1
  • 12
  • 21
  • if you could .. can you please have a look at my this other issue :https://stackoverflow.com/questions/64858171/how-to-read-two-text-file-into-a-class-object-and-write-them-to-one-text-file?noredirect=1#comment114672120_64858171 – kunga dolma Nov 16 '20 at 15:46