Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to read a String .dat file into an array

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to read a String .dat file into an array

    Hello,

    I am tasked with coding a program that allows the user to guess a randomized word from an array with 10 slots. The pre-chosen words are stored in a wordguess.DAT file and must be imported into a string array. I have looked on other Java forums and most of the examples use methods that I have not learned yet, so it is hard for me to understand what is really going on. Can anyone help?

    	public static void init()
    	{
    		//sets up the console scanner
    		Scanner myScanner = new Scanner(System.in);
    		myScanner.useDelimiter(System.getProperty("line.separator"));
     
    		//sets up the in-file along with the in-file scanner
    		//inFile = new File("wordguess.dat");
    		//datScanner = new Scanner(inFile);
    		//datScanner.useDelimiter(System.getProperty("line.separator"));
     
    		//read input into an array with a try-catch
    		try
    		{
    			wordArray = new ArrayList<String>();
    			Scanner datScanner = new Scanner(new File("wordguess"));
    			while(datScanner.hasNextLine());
    			{
    				wordArray.add(datScanner.nextLine());
    			}
    		}
     
    		catch(IOException e)
    		{
    			System.out.println("File not found.");
    		}
     
    	}


    --- Update ---

    My teacher doesn't want us to use ArrayList since we have not talked about it yet in class. He wants the array to be basic.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: How to read a String .dat file into an array

    What part are you having prblems with?
    Defining an array
    reading a word from the file
    assigning that word to a slot in the array
    making a loop what will enclose the reading and assigning steps

    What statements in the posted code can be used in your assignment?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to read a String .dat file into an array

    Quote Originally Posted by Norm View Post
    What part are you having prblems with?
    Defining an array
    reading a word from the file
    assigning that word to a slot in the array
    making a loop what will enclose the reading and assigning steps

    What statements in the posted code can be used in your assignment?
    I can define the array and I can read a word from the file, but I don't know how to assign said word to a slot in the array or making the loop that will enclose the reading and assigning steps. Well, apparently, I can't use anything from the code I pasted... as what my teacher says.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to read a String .dat file into an array

    Oh, okay! I see now. Major brain fart there.

Similar Threads

  1. Fastest way to read and search a string in a large file using core java
    By patilsn_jay in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 26th, 2012, 04:35 AM
  2. Fastest way to read and search a string in a large file using java
    By patilsn_jay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 09:29 AM
  3. HW-how to read a string from a text file
    By yaboibangz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2012, 10:34 AM
  4. How to read a 2 dimensional text file from an array?
    By seaofFire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2012, 08:44 AM
  5. Need help with reading from .dat file
    By cyoung in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 26th, 2011, 08:34 AM

Tags for this Thread