Results 1 to 6 of 6
  1. #1
    Isshin is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default Trying to write to a file with a scanner, lots of errors

    I'm trying to put a Name, number, and any notes to a 'person' in a file.


    However I always get an error, when i "fix" one thing it comes up as a different error and I'm not sure what to do. The first if statement is where it pops up, I haven't even started to mess with the output.

    Any help would be greatly appreciated.

    (and I have a file made for it)





    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Phonebook {
    
    
    
    	public static void main(String[] args) {
    
    		String rawr = "";
    		String Find = "";
    
    		File F = new File("MyPhoneBook.txt");
    		Scanner S = null;
    		Scanner stdin = new Scanner(System.in);
    
    		Phonebook_data[] P = new Phonebook_data[100];
    
    		System.out.println("Use e for enter, f for find, l for list, q to quit.");
    		System.out.println("");
    
    		for (int z = 0; z < 100; z++) {
    
    			System.out.print("Enter Command: ");
    			rawr = stdin.next();
    			rawr = rawr.toLowerCase();
    
    			if (rawr.equals("e")) {
    
    				try {
    					P[z] = new Phonebook_data();
    					S = new Scanner(F);
    
    					System.out.print("Enter Name: ");
    					P[z].Name = S.next();
    					System.out.print("Enter Number: ");
    					P[z].Number = S.next();
    					System.out.print("Enter Notes: ");
    					P[z].Notes = S.next();
    				}
    
    				catch (FileNotFoundException e1) {
    					e1.printStackTrace();
    				}
    
    			}
    
    			if (rawr.equals("f")) {
    
    				FileOutputStream out = null;
    				try {
    					out = new FileOutputStream("MyPhoneBook.txt");
    				} catch (FileNotFoundException e) {
    					e.printStackTrace();
    				}
    				PrintStream ps = new PrintStream(out);
    				for (int i = 0; i < 100; i++) {
    					Find = stdin.next();
    					if (P[i].equals(Find))
    						ps.print(P[i].Name);
    
    				}
    			}
    
    			if (rawr.equals("q")) {
    				System.out.println("Goodbye.");
    				System.exit(1);
    			}
    
    		}
    	}
    }
    There's also

    Java Code:
    public class Phonebook_data{
    	
    	public String Name;
    	public String Number;
    	public String Notes;
    }

    Java Code:
    Enter Command: e
    Enter Name: Exception in thread "main" java.util.NoSuchElementException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at Phonebook.main(Phonebook.java:47)

  2. #2
    Harush is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    I'm new to java myself.

    Where you have "S = new Scanner(F);"

    do you need to use "S = new Scanner(new FileInputStream(F));"?

    I'm not sure...

  3. #3
    Isshin is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Keep getting the "NoSuchElementException"

    Thanks though

  4. #4
    Harush is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    NoSuchElement means you've hit the last record and it's trying to keep reading.

  5. #5
    Isshin is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Hmm. Should it matter if the file was empty if it was just created and its supposed to be accepting input?

    I filled it with a bunch of numbers, now i'm getting this problem.



    Java Code:
    Enter Command: e
    Enter Name: Enter Number: Enter Notes: Enter Command:
    This is the section i'm trying to figure out.

    Java Code:
    				try {
    					P[z] = new Phonebook_data(Name, Number, Notes);
    					S = new Scanner(new FileInputStream(F));
     
    					System.out.print("Enter Name: ");
    					P[z].Name   = S.next();
    					System.out.print("Enter Number: ");
    					P[z].Number = S.next();
    					System.out.print("Enter Notes: ");
    					P[z].Notes  = S.next();
    				}

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You seem confused on how to use a Scanner. If you want to get input from the user on the command line, then you pass System.in into the Scanner's constructor, and you then you prompt the user with System.out.print(...) or System.out.println(...) and process the user's input with the Scanner.

    If you want to get information from a File, you pass the File into the Scanner's constructor, and then use a while(myScanner.hasNextLine()) to loop through the file calling myScanner.nextLine() once within the loop (there are variations on this if you want to get only ints and such), but you don't prompt the user for input from the command line when trying to extract information from a File. It just doesn't make sense.

Similar Threads

  1. Using Read/Write From File Using Scanner
    By javaisntcoffee123 in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 04:35 AM
  2. Using Scanner class to write to Vectors
    By JonoF in forum New To Java
    Replies: 16
    Last Post: 04-16-2009, 04:46 AM
  3. 2 errors with Scanner , help
    By enghar in forum New To Java
    Replies: 3
    Last Post: 01-25-2009, 04:03 AM
  4. 2 errors with Scanner , help
    By enghar in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 10:34 PM
  5. Lots of my folders have become read-only
    By Manoeuvre in forum Eclipse
    Replies: 5
    Last Post: 12-02-2008, 01:48 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
  •