Go Back   Java Programming Forums > Java Standard Edition Programming Help > Java SE APIs


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 19-11-2009, 08:58 AM
Junior Member
 

Join Date: May 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Sterzerkmode is on a distinguished road
Default delimiter question

I have a file that contains

1 2 3
4 5 6
7 8 9

I have a scanner that reads it, puts it into a String, and then attempts to use the split method to tokenize it. The problem is that it isn't counting \n as a delimiter.

Java Code
try {
            Scanner scan = new Scanner(theFile);
            while(scan.hasNextLine()) {
                n++;
                stringForm += scan.nextLine();
            }   //end while

            //make array
            square = new int[n][n];
            //put ints into array
            String[] tokens = stringForm.split("\\s");
            for(int i=0;i<tokens.length;i++) {  //loop through array
                    for(int rowN=0;rowN<n;rowN++) {
                        for(int colN=0;colN<n;colN++) {
                            System.out.println(tokens[i]);
                            square[rowN][colN] = Integer.parseInt(tokens[i]);   //initialize array
That is the relevant code. I was under the impression \\s would cover all whitespace which includes \n. Can anyone help me?



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 19-11-2009, 10:21 AM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
Default Re: delimiter question

This is because you are using nextLine(). Here's a quote from the API

Quote:
Originally Posted by Java API
This method returns the rest of the current line, excluding any line separator at the end.
As you can see, nextLine() doesn't return a linefeed. So you cannot parse it if it isn't there.

Regards,
Chris
__________________
chris[at]javaprogrammingforums[dot]com

University of Wales, Bangor
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Question lotus Loops & Control Statements 3 20-06-2009 08:30 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jbutton java jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:49 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.