I need to skip white spaces while reading strings from a file

I need help to have the scanner instance reading my file to skip white

Page 1 of 1

2 Replies - 17740 Views - Last Post: 03 March 2008 - 05:39 PM Rate Topic: -----

#1 shadow_666  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-January 08

I need to skip white spaces while reading strings from a file

Posted 03 March 2008 - 04:29 PM

I am using an instance of the scanner class to read a file. This file contains somtimes information such as a full name. Which, contains more than two strings, or an address. I need to able to skip white spaces. For example, when I am reading an address, which contains more than two strings. I should be able to skip white spaces to read the full address or or full name before the scanner moves to the next string. ???????
I know it looks pretty basic, but i have a very sorry book!!!!
java.io.File file = new java.io.File("test.txt");
		
		 //creates a Scanner for the file
		 java.util.Scanner input = new java.util.Scanner(file);
		
		 while(input.hasNext()){
			
	objName = input.next();
	if(objName.equals(vehicle)){
		ownerName = input.next();
		address = input.next();
		phone = input.next();
		email = input.next();
	}//first end fi

		}


Is This A Good Question/Topic? 0
  • +

Replies To: I need to skip white spaces while reading strings from a file

#2 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6095
  • View blog
  • Posts: 10,531
  • Joined: 28-September 07

Re: I need to skip white spaces while reading strings from a file

Posted 03 March 2008 - 05:28 PM

readChar() will skip white space.

Another option might be to use a comma delimited input file (*.csv) rather than text.
Was This Post Helpful? 0
  • +
  • -

#3 n_b  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 09-August 07

Re: I need to skip white spaces while reading strings from a file

Posted 03 March 2008 - 05:39 PM

There are many ways to skip whitespace, here are some:

1. If you only have leading or trailing whitespace, use the trim() method of the String class, which does exactly that. If there are no such whitespace, the original string is returned, unchanged.

2. If you're familiar with regular expressions, you can use the split() method of the String class. It's signature is split(String regexp). It returns an array of strings that is given by splitting the original string around matches of the expression regexp.

For example:
"aa#bb#cc".split("#");

will return the array
["aa", "bb", "cc"]

Splitting on whitespace should do what you want, and whitespace is represented by the regular expression "\s". This should eliminate all whitespace in your text, including newlines, if you want something else then read about regular expressions in Java. You should look up the class Pattern in the Java API documentation.

3. While we're on the subject, the Scanner class can use regular expressions too. Use the method next(String regexp) or next(Pattern p). Just tell it to give you the next thing that is not whitespace:
String nextToken = myScanner.next("\S");

The "\S" pattern matches anything that is not whitespace.

If the above examples didn't help you, then you must be a bit more precise. I quote:

Quote

. . . For example, when I am reading an address, which contains more than two strings. I should be able to skip white spaces to read the full address. . .

What do you mean? There must be some format or specification for the file. If not your task will be very difficult indeed.

For example: If the file looks like this:
John Doe Sunset Boulevard 12

all in one line, how do you tell what part is the name and what part is the address? If, on the other hand, all items are on their own line, your life will be very much simpler:
John Doe
Sunset Boulevard 12


because you know that the first line always contains the name and the address is always on the second line. Then you can simply use the nextLine() method of the Scanner class.
Was This Post Helpful? 1

Page 1 of 1