Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6563485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:51:27+00:00 2026-05-25T13:51:27+00:00

BufferedReader and Scanner’s nextLine() seem to be helping a little too much by removing

  • 0

BufferedReader and Scanner’s nextLine() seem to be helping a little too much by removing all trailing whitespace. I need to preserve columns, which at the moment are allowed to be empty values, but hesitate to loop through each row using next() or getBytes() identifying tab characters since there could potentially be millions of rows with hundreds of columns.

Are there alternatives to these two methods that I’m missing for reading lines?
Are there flags or any other options to set in these methods to preserve whitespace?
Do I simply force the user to use none-blank fields?
I’m not alone in trying to preserve whitespace am I?

I have a problem with it when it’s reading from a file. I have this code

import java.lang.*;
import java.util.*;
import java.io.*;

public class stringTest
{
   public static void main (String[] args) throws IOException
   {
        BufferedReader br = new BufferedReader(new FileReader("wtf.txt"));
        String l = br.readLine();
        while (l != null) {
            System.out.println(l.split("\t").length);
            l = br.readLine();
        }
   }
}

wtf.txt contains

h\tu\tr\tf\n
o\tm\tg\t\t\n

And the output is

4
3

Additionally, if I add a line anywhere that is all tabs, ie

h\tu\tr\tf\n
\t\t\t\t\t\n
o\tm\tg\t\t\n

The output is

4
0
3

I don’t think it’s an issue with split because if I use the code

String s = "w\tt\tf\t\t\n";
System.out.println(""+s.split("\t").length);
String s1 = "w\tt\tf\tx\n";
System.out.println(""+s1.split("\t").length);
String s2 = "\t\t\t\t\t\t\n";
System.out.println(""+s2.split("\t").length);

The output is

5
4
6
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:51:28+00:00Added an answer on May 25, 2026 at 1:51 pm

    EDIT: It sounds like your problem is to do with split, not BufferedReader or Scanner. You can take those out of the equation very easily:

    public class Test {
        public static void main(String[] args) {
            String line = "\t\t\t";
            System.out.println(line.split("\t").length); // Prints 0
        }
    }
    

    There are various different ways of splitting a string on delimiters – you might want to look at the Splitter class in Guava:

    import java.util.List;
    import com.google.common.base.Splitter;
    import com.google.common.collect.Lists;
    
    public class Test {
        public static void main(String[] args) {
            Splitter splitter = Splitter.on('\t');
            String line = "\t\t\t";
            List<String> bits = Lists.newArrayList(splitter.split(line));
            System.out.println(bits.size()); // Prints 4
        }
    }
    

    BufferedReader.readLine() doesn’t remove trailing tabs, certainly. Sample code:

    import java.io.*;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            // Not closing anything just for convenience
            String text = "a\tb\t\r\nc\td\t";
            BufferedReader reader = new BufferedReader(new StringReader(text));
    
            String line;
    
            while ((line = reader.readLine()) != null)
            {
                System.out.println(line.replace("\t", "<tab>"));
            }
        }
    }
    

    Output:

    a<tab>b<tab>
    c<tab>d<tab>
    

    Ditto Scanner.nextLine():

    import java.io.*;
    import java.util.*;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            // Not closing anything just for convenience
            String text = "a\tb\t\r\nc\td\t";
            Scanner scanner = new Scanner(new StringReader(text));
    
            while (scanner.hasNextLine())
            {
                String line = scanner.nextLine();
                System.out.println(line.replace("\t", "<tab>"));
            }
        }
    }
    

    (Same output.)

    So whatever’s stripping your whitespace, it isn’t Scanner.nextLine() or BufferedReader.readLine().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader(rates.txt))); for (int
I am using Scanner and BufferedReader objects to read text input from a url,
Im used too using Scanner mainly and want too try using a buffered reader:
I currently have 2 BufferedReader s initialized on the same text file. When I'm
I currently use: BufferedReader input = new BufferedReader(new FileReader(filename)); Is there a faster way?
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
Is there a way to place a BufferedReader into a String in one shot,
Is there a way to read a ByteBuffer with a BufferedReader without having to
I'm reading a local file using a BufferedReader wrapped around a FileReader: BufferedReader reader
I have 2 sockets and I am using BufferedReader around it's InputStreams. What I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.