Hello, I'm a beginner at Java and trying to get to know programming and one day work my way up developing android apps. Well I was trying make this program that would read in data from a txt file like this:

[img]http://i42.tinypic.com/2vvkokj.png[/img]

I wanted the firstname, lastname, id# and dob each to have their own variables and then only to have the lastname and dob be printed to an outputfile(that the user would name) like this:

[img]http://i44.tinypic.com/sfgjkl.png[/img]

so I decided the best way to do that would be in arrays. I ran into a problem during my first for loop after the trybox where it says that the first array has no source so I tried to move the for loop to include the trybox and that still wouldn't work..

I received these errors:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at HealthDriver.main(HealthDriver.java:38)


After commenting out some code I took out the forloop completely and I just in left the arrays initialized to [0] and it worked until the 2nd for loop and would instead give me the trybox exception error message(in the 2nd trybox)...Could anyone tell me what's wrong here? or maybe someone has an idea how to do this without arrays?

Any tips, nudges in the right direction, constructive criticism is welcomed. Thank you all in advanced. :D!

import java.util.*; 
import java.io.*;  //Include header 
public class HealthDriver {

    public static void main(String[] args) {
       
    String writeword; //used to write filename to outputfile
   	 String[] aryfirst = new String[4];
   	 String[] arylast= new String[4];
   	 int[] aryid = new int[4];
   	String[] arydob = new String[4]; 
        
    	//Prompt to enter in data.
    	 System.out.printf("Please enter in the name of the file:  ");

    	Scanner s = new Scanner(System.in);


    	//Reads in the string name for the file.
    	writeword = s.next();
    	//Reads in from the file Patient.txt
         String filename = "Patient.txt";
         Scanner sf = null;
     	
	try
	{
 		sf = new Scanner(new FileReader(filename));
	}
	catch (Exception e)
	{
	System.out.printf("ERROR...There is no Patient.txt");
	
	}
	  //Loop that reads each piece of data from txt and arranges them in each array.
	 int i = 0;  
       for (i = 0; i <=4; i++)
       {
    	 aryfirst[i] = sf.next();
    	 arylast[i] = sf.next();
    	 aryid[i] = sf.nextInt();
    	 arydob[i] = sf.next();
     }
    			
    	    	//Declare a PrintStream variable
    	    	PrintStream ps=null;  
    	    	try
    	    	{
    	    		//This creates a new file or wipes the old one. Based on what user types
    	    		ps = new PrintStream(writeword);
    	    		  //Loop that should print out each last name and date of birth..
    	    	       for (i = 0; i <=4; i++)
    	    	       {
    	    			  ps.printf("%-20s", arylast[i]);
    	    			  ps.printf("%15d", arydob[i]);
    	    	       }
    	    			  /*
    	    		    	 aryfirst[i] = sf.next();
    	    		    	 arylast[i] = sf.next();
    	    		    	 aryid[i] = sf.nextInt();
    	    		    	 arydob[i] = sf.next();
    	    		    	 ps.printf("%-20s", lastname);
    	    		    	 */	
    	    		//ps.printf("%15d", dateofbirth);   
    	    	}
    	    	catch (Exception e)
    	    	{
    	    		System.out.println("ERROR! Something went WRONG!");
    	    	}
     }

   
    }

can you post some code lines to show what is in your patient.txt file?

also, you may want to investigate your code a bit further: for instance, if you don't have a patient.txt file, what do you think will happen on line 38?
since you didn't instantiate sf, you'll get a NullPointerException.

can you post some code lines to show what is in your patient.txt file?

also, you may want to investigate your code a bit further: for instance, if you don't have a patient.txt file, what do you think will happen on line 38?
since you didn't instantiate sf, you'll get a NullPointerException.

No I have it the file. I was giving the img as an example but here is the actual txt file.

Derek		Jeter		1	6/26/1974
Alex		Rodriguez	2	7/27/1975
Mark		Texiera		3	4/11/1980
Robinson	Cano		4	10/22/1980

also isn't sf instantiated on like 23?

and I wanted the output file it look something like this

Lastname	        Date of Birth
                Jeter		        6/26/1974
		Rodriguez		7/27/1975
		Texiera			4/11/1980
	        Cano			10/22/1980

here's your problem:

for (i = 0; i <=4; i++)

you have to understand that, since your arrays have only four elements, and since they are 0 based (first index = 0,not 0), the highest index possible = 4 - 1

for (i = 0; i < 4; i++)

well .. it may not be your only problem, but this is the first one solved anyway :)

also isn't sf instantiated on like 23?

nope, you're setting it to null, which is not an instance.
null is the default value every Object has, when it's not yet instantiated. but since it's not an instance, you can't call any of the instance methods on it.

you instance your sf variable on line 27, but only if no exception is thrown there.
you should write your code so, that all the calls made on sf are also placed within that try block, that way you eliminate the possibility of them being called when sf is not isntantiated.

here's your problem:

for (i = 0; i <=4; i++)

you have to understand that, since your arrays have only four elements, and since they are 0 based (first index = 0,not 0), the highest index possible = 4 - 1

for (i = 0; i < 4; i++)

well .. it may not be your only problem, but this is the first one solved anyway :)

Thanks! I feel so silly.. I know that about arrays too!!... and Ive been staring at this code for hours now...that's a bit discouraging -_-. Thanks so much!! I edited it and I'm now just getting the 2nd exception error message that I put in place. I'll see if I can try to find out what's wrong with the last trybox...I'm glad the first error is gone. If you can find the last one that would be a great help as well. :D I'm close I CAN FEEL IT!

well, your first error there, is once agian <= 4.
next to that, it looks like there's a problem with your printf's

Yeah, I saw that right after you posted it the first time and changed that to 3 instead of 4. Then I commented out

ps.printf("%15d", arydob[i]);

and it complied yet the output only produced

Jeter

mhm..something logically must be wrong. I'm thinking all the values aren't being stored in the array. Back to the drawing board.

it's not something logically, you're printing Strings, so set s instead of d.
try this:

ps.printf("%20s %15s", arylast[i], arydob[i]);
    ps.println();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.