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 12 of 12

Thread: Files and arrays

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Files and arrays

    Practicing a lot of java and testing my codes until I become better at it.

    Am reading records from a txt file and storing it into an array
    import java.util.*;
    import java.io.*;
     
    public class PatientExercise {
    	//patients exercise time
    	public static void main (String[]args) throws IOException{
    		Scanner in = new Scanner(new FileReader("values.txt"));
    		double [] patientTimeRecords = new double [300];
    		int noExerciseCount=0, numPatients =0;
    		double highest=0, lowest=0, avg=0, totalTime=0;
     
    		//iniitializing the array
    		for(int i = 0; i < patientTimeRecords.length; i++)
    			patientTimeRecords[i] = 0.0;
     
     
        	int i=0;
    		patientTimeRecords[i] = in.nextDouble();
    		//double []patientTime = double
    		while(patientTimeRecords[i] >= 0)
    		{
    			totalTime = totalTime + patientTimeRecords[i];
     
    			//counts number of times patient didn't exercise
    			if (patientTimeRecords[i] == 0)
    			{
    				noExerciseCount++;
    			}
    	        //finds and updates highest exercise time
    			if (patientTimeRecords[i] > highest)
    			{
    				highest=patientTimeRecords[i];
    			}
    			 //finds and updates lowest exercise time
    			if (patientTimeRecords[i] < lowest)
    			{
    				lowest=patientTimeRecords[i];
    			}
     
    			 numPatients++;
    			 patientTimeRecords[i]= in.nextDouble();
    		}//end while
     
    		avg =totalTime/numPatients;
    		System.out.printf("Highest Time: %2.2f\n", highest);
    		System.out.printf("Lowest Time: %2.2f\n", lowest);
    		System.out.printf("Average Time: %2.2f\n", avg);
    		System.out.printf("Number of times patient did not exeercise: %2.2f\n", noExerciseCount);	
     
     
    	}
    }


    However an error msg keeps popping up:
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)
    at pastpapers.PatientExercise.main(PatientExercise.ja va:44)

    line 44 is:patientTimeRecords[i]= in.nextDouble();


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Files and arrays

    You never increment i in your while loop. Even if you did that you rely on the file containing at least 300 doubles.

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Files and arrays

    Hi, i incremented the i by replacing the:
    patientTimeRecords[i]= in.nextDouble();
    TO
    patientTimeRecords[i]++;

    But, my output screen is blank

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Files and arrays

    patientTimeRecords[i]++;
    That code increments the contents of the array indexed by i. It does NOT change the value of i.
    To increment i, move the ++ to next to the i: ...[i++]
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Files and arrays

    still not getting it to wrk :/ i ended up placing the for loop in the while and it created more problems.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Files and arrays

    Post the new code and describe the problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Files and arrays

    import java.util.*;
    import java.io.*;
     
    public class PatientExercise {
    	//patients exercise time
    	public static void main (String[]args) throws IOException{
    		Scanner in = new Scanner(new FileReader("values.txt"));
    		double [] patientTimeRecords = new double [300];
    		int noExerciseCount=0, numPatients =0;
    		double highest=0, lowest=0, avg=0, totalTime=0;
     
    		//iniitializing the array
    		//for(int i = 0; i < patientTimeRecords.length; i++)
    			//patientTimeRecords[i] = 0.0;
     
             int i=0;
        	//reads patient time from records
    		patientTimeRecords[i] = in.nextDouble();
    	    //file ends when number is negative
    		while(patientTimeRecords[i] >= 0)
    		{
     
    			for(int i = 0; i < patientTimeRecords.length; i++)
    			{
    				patientTimeRecords[i] = in.nextDouble();
     
    			totalTime = totalTime + patientTimeRecords[i];
     
    			//counts number of times patient didn't exercise
    			if (patientTimeRecords[i] == 0)
    			{
    				noExerciseCount++;
    			}
    	        //finds and updates highest exercise time
    			if (patientTimeRecords[i] > highest)
    			{
    				highest=patientTimeRecords[i];
    			}
    			 //finds and updates lowest exercise time
    			if (patientTimeRecords[i] < lowest)
    			{
    				lowest=patientTimeRecords[i];
    			}
    			}
    			 numPatients++;
    			 patientTimeRecords[i]=in.nextDouble();
    		}//end while
     
    		avg =totalTime/numPatients;
    		System.out.printf("Highest Time: %2.2f\n", highest);
    		System.out.printf("Lowest Time: %2.2f\n", lowest);
    		System.out.printf("Average Time: %2.2f\n", avg);
    		System.out.printf("Number of times patient did not exeercise: %2.2f\n", noExerciseCount);	
     
     
    	}
    }


    the for loop said i is duplicate but when i remove 1=0 before the while loop, the program says i cannot be resolved as variable.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Files and arrays

    i is duplicate
    Change the name of the variable, say to i2 so there are two variables: i and i2.

    You need to copy and paste the full text of the error messages because they have important information that you are leaving out in your posts.

    BTW The poor formatting of the code is making it hard to see where the pairs of {} are located.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Files and arrays

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Duplicate local variable i

    at pastpapers.PatientExercise.main(PatientExercise.ja va:25)
    line 25:the for loop line

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Files and arrays

    Did you try renaming that variable so it had a new name and was not a duplicate?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Files and arrays

    yup, everytime I adjust something, another problem arises. Nevermind

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Files and arrays

    Ok, glad you are making progress.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    Java girl (April 15th, 2014)

Similar Threads

  1. Arrays and files
    By Java girl in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2014, 07:31 PM
  2. Replies: 3
    Last Post: August 3rd, 2012, 10:40 AM
  3. Java Picture Processor with PGM files and Multi Arrays
    By snow_mac in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2011, 09:55 PM
  4. Having an issue reading files to create two arrays.
    By ccrosby in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 26th, 2011, 06:47 AM