Results 1 to 6 of 6
  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Scanner.nextLine ??

    :p I'm posting here so I can see the problem more clearly. I don't know why the nextLine method is getting "null" in this case. I switched to Scanner from BufferedReader because I was getting this same problem. (and because Scanner seems easier for a noob) From this code you can see that the getCustomLesson method is working. You can also see that the Scanner is opening the file and reading it. But I don't understand why it's writing null values to my array. I wrote the readIt method at the top just to see if the values were actually there. There is no Stack Trace exceptions, only this:
    Jane line 0
    Janet line 0
    talks line 0
    whispers line 0
    loudly line 0
    sweetly line 0
    quietly line 0
    null line 1
    null line 2
    null line 3
    null line 4
    null line 5
    null line 6
    null line 7
    null line 8
    null line 9
    null line 10
    null line 11

    Maybe seeing the problem here will open my eyes... or maybe I'll need some help from you kind folks.
    Java Code:
    public class TestAnything {
    public static void main(String [] args){
    		 String file = AlphaFileChooser.getCustomLesson();
    		 String[] theFile = readIt(file, 12);
    		 readIt(theFile);
    }
    public static void readIt(String[] arrayToRead){
    		int lineCount = 0;
    		while (lineCount < arrayToRead.length){
    			System.out.println(arrayToRead[lineCount] + " line " + lineCount);	
    			lineCount++;
    		}	
    }			  
    public static String[] readIt(String path, int arraySize){
    		File file = new File(path);
    		String[] tempArray= new String[arraySize];
    		Scanner sc;
    		try {
    			int i = 0;
    			sc = new Scanner(file);
    			while (sc.hasNextLine()){
    				tempArray[i]= sc.nextLine();
    				System.out.println(sc.nextLine() + " line " + i);
    			}
    		sc.close();
    		}
    		catch (FileNotFoundException e) {
    		e.printStackTrace();
    		}
    		return tempArray;	 
    	 }			 
    }

  2. #2
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Scanner.nextLine ??

    OK, that did help. I wasn't incrementing "i" in the while loop. On to the next boneheaded mistake. ;p

    No, still some strangeness... the file I'm reading from looks like this:
    Jim
    Jane
    Joe
    Janet
    sings
    talks
    yells
    whispers
    softly
    loudly
    quietly
    sweetly

    I now get output from running this test program of this:
    Jane line 0
    Janet line 1
    talks line 2
    whispers line 3
    loudly line 4
    sweetly line 5
    Jim line 0
    Joe line 1
    sings line 2
    yells line 3
    softly line 4
    quietly line 5
    null line 6
    null line 7
    null line 8
    null line 9
    null line 10
    null line 11

    Seems it is only writing every other line... looking again...
    Last edited by NotTooOld; 05-07-2013 at 08:24 AM.

  3. #3
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Scanner.nextLine ??

    OK! I have it working the way I want it to now. YAY!
    Read from this text file:

    Jim's brother
    Jane
    Joe
    Janet's sister, Judy
    sings
    talks
    yells
    whispers
    softly
    loudly
    quietly
    very sweetly

    This code:

    Java Code:
    public class TestAnything {
    public static void main(String [] args){
    //getCustomLesson just opens a JFileChooser and returns the file path.
    		 String file = AlphaFileChooser.getCustomLesson();
    		 String[] theFile = readIt(file, 12);
    		 readIt(theFile);
    }
    public static void readIt(String[] arrayToRead){
    		int lineCount = 0;
    		while (lineCount < arrayToRead.length){
    			System.out.println(arrayToRead[lineCount] + " index " + lineCount);	
    			lineCount++;
    		}	
    }			  
    public static String[] readIt(String path, int arraySize){
    		File file = new File(path);
    		String[] tempArray= new String[arraySize];
    		Scanner sc;
    		try {
    			int i = 0;
    			sc = new Scanner(file);
    			// Added this so I would get an entire line, not
    			// just one word.
    			sc.useDelimiter("\n");  
    			while (sc.hasNext()){
    				tempArray[i]= sc.next();
    				System.out.println(tempArray[i] + " line " + i);
    				i++;
    			}
    		sc.close();
    		}
    		catch (FileNotFoundException e) {
    		e.printStackTrace();
    		}
    		return tempArray;	 
    	 }			 
    }
    I now get the output I wanted... though that "\n" delimiter messed up the format for my System.out stuff. Who cares! The main thing is it is writing to the array as I want it to.

    Output:

    Jim's brother
    line 0
    Jane
    line 1
    Joe
    line 2
    Janet's sister, Judy
    line 3
    sings
    line 4
    talks
    line 5
    yells
    line 6
    whispers
    line 7
    softly
    line 8
    loudly
    line 9
    quietly
    line 10
    very sweetly
    line 11
    Jim's brother
    index 0
    Jane
    index 1
    Joe
    index 2
    Janet's sister, Judy
    index 3
    sings
    index 4
    talks
    index 5
    yells
    index 6
    whispers
    index 7
    softly
    index 8
    loudly
    index 9
    quietly
    index 10
    very sweetly
    index 11
    Last edited by NotTooOld; 05-07-2013 at 10:14 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Scanner.nextLine ??

    If you don't want the delimiter then stick to using nextLine().
    Using next() didn't fix your earlier problem...not using sc.nextLine() twice int he loop (once to assign to tempArray[i] and the second time in the println) did.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Scanner.nextLine ??

    Quote Originally Posted by Tolls View Post
    If you don't want the delimiter then stick to using nextLine().
    Using next() didn't fix your earlier problem...not using sc.nextLine() twice int he loop (once to assign to tempArray[i] and the second time in the println) did.
    Oh you arrrrrrrrr good! I'll go in and fiddle with it some more now <fingers crossed that I don't hose it up> So all I really needed to do was take out the System.out stuff and it would have worked. Live (and listen) and learn.


    Strangest of all, at least to me, is that I originally tried using a method that reads the lesson files that are included with the program. For some reason I couldn't get that method to read the saved custom lessons. Both type lesson are just txt files. The only difference I can locate is that I hardcoded the constructor for the included lessons since I already know the file names, and to let the user just pick from a dropdown list. I take that back. All the paths I send to the first method are relative paths. I have to use canonical paths for the custom lessons because I don't know where the user will want to save them. Is it possible that this method will only accept relative paths?
    Java Code:
    	public static String[] fileToStringArray(String path, int arraysize){
    		String[] list = new String[arraysize];
    				
    		try {
    			InputStream inputStream = Utility.class.getResourceAsStream(path);
    			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    			try {
    				String line = reader.readLine();
    				int i = 0;
    				while (line != null) {
    					list[i] = line;
    					line = reader.readLine();
    					i++;
    				}
    			}
    			finally {
    				inputStream.close();
    				reader.close();
    			}
    		}
    		catch (IOException e){
    			e.printStackTrace();
    			JOptionPane.showMessageDialog(null, "An Error occured attempting " +
    					"to open the file:   \n" + path);
    		}
    		return list;
    	}
    My entire silly little program is about finished now. Just some text editing to do.

    Thanks bro!
    Last edited by NotTooOld; 05-07-2013 at 01:14 PM.

  6. #6
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Scanner.nextLine ??

    Quote Originally Posted by Tolls View Post
    If you don't want the delimiter then stick to using nextLine().
    Using next() didn't fix your earlier problem...not using sc.nextLine() twice int he loop (once to assign to tempArray[i] and the second time in the println) did.
    You were absolutely right. Works like a charm now.

    Thanks again.

Similar Threads

  1. Scaner NextLine()
    By Dex in forum New To Java
    Replies: 3
    Last Post: 08-14-2012, 01:07 PM
  2. Scanner "nextLine" Command [Homework]
    By btgrant in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 02:45 AM
  3. nextLine issue
    By paul1024 in forum New To Java
    Replies: 3
    Last Post: 03-24-2011, 11:48 PM
  4. Replies: 3
    Last Post: 03-23-2011, 11:16 AM
  5. Problem Of Scanner Object with its method nextLine()
    By Cluster Storm in forum AWT / Swing
    Replies: 12
    Last Post: 06-17-2010, 05:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •