Results 1 to 19 of 19
  1. #1
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Unhappy How to add values to an array?

    PLEASE SOMEONE HELP T^T

    I have to go through a file and add all the values into an array and then print the array.

    Here's my code:

    Java Code:
    package apcsChapter6;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class HeatIndexV1_2 {
    
    	/**
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    
    		int countNum = 0;// to count how many numbers are in the array
    		int count = 0;// to hold a value for the for loop
    		String txtDataOne = ""; // value for file input
    		// String txtDataTwo = ""; //value for second file input
    
    		String[] month = { "Jan", "Feb", "Mar", "Apr", "Jun", "Jul", "Aug",
    				"Sept", "Oct", "Nov", "Dec" };
    		for (String monthPrint : month) {
    			System.out.printf("%5s", monthPrint);
    		}
    		System.out
    				.println("\n**********************************************************");
    
    		File humidFile = new File("KeyWestHumid.txt");
    		Scanner inHumid = new Scanner(humidFile);
    
    		// Counting how long the array should be
    		while (inHumid.hasNext()) {
    			countNum += 1;
    		}
    		inHumid.close();
    
    		// Initialize the array
    		int[] humidArray = new int[countNum];
    		// parse the values of the text files and put them into the text files
    
    		while (inHumid.hasNext()) {
    			count += 1;
    			txtDataOne = inHumid.next();
    			for (int x = 0; x < 2; x++) {
    				humidArray[count] = Integer.parseInt(txtDataOne);// to place the
    																	// value of
    																	// number
    			}
    
    		}
    		inHumid.close();
    		
    		for(Integer i: humidArray){
    			i = Integer.parseInt(txtDataOne);
    			System.out.printf("f%5d", humidArray);
    		}
    
    		/*
    		 * for(Integer myStuff : humidArray){ System.out.printf("%5d", myStuff);
    		 * }
    		 */
    
    	}
    
    }

    And here's what my assignment says to do:

    nstructions: Write a program to calculate the monthly Heat Index for a specific city.
    1. Create a new project called 6.03 Weather Data in the Mod06
    Assignments folder.
    2. Create a class called HeatIndex in the newly created folder.
    3. Be sure you have downloaded the KeyWestTemp.txt and the KeyWestHumid.txt files to the
    project folder.
    4. Break the program into functional units so that you can use separate for loops and arrays to
    handle specific tasks.
    5. Your program should read in the data from the two text files.
    6. Be sure to use the for-each loop when it is appropriate.
    7. Calculate the monthly Heat Index for Key West, Florida.
    8. Notice from the table shown in the 6.03 Virtual Lecture Notes that if the temperature and
    humidity are below certain levels, the actual temperature and the apparent temperature (i.e.
    the Heat Index) are identical. For example, in the expected output shown below, notice that
    the Heat Index is only calculated for the months of May – October.
    9. Use the Formatting Grid you downloaded in the last lesson to plan your user-friendly
    output.
    10. Print the results in a user-friendly format (i.e. use printf()) with columns for the Month,
    Temperature, Relative Humidity, and the Heat Index.
    11. Write the Heat Index data to a file. Include this file when you submit your program.
    12. Writing the lengthy Heat Index formula can be very error prone, so watch out for the Order
    of Operations.
    I know how to scan the file but I have no idea how to add the values of each number it encounters as it goes along to each thing in the array D: someone please help it's been three days T^T

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How to add values to an array?

    You didn't exactly explain what problem or error message you are seeing. However, I did observe that you closed inHumid but continued to try to read from it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to add values to an array?

    The error, is that I have no idea how to get the numbers from the text file into my array so all the things that I've tried have resulted in a program that just prints out the months :/

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How to add values to an array?

    You opened the file to count the records so you initialize the array, correct? Then you closed the file (line 36). Then you try to read from it without re-opening it (line 44).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to add values to an array?

    Quote Originally Posted by jim829 View Post
    You opened the file to count the records so you initialize the array, correct? Then you closed the file (line 36). Then you try to read from it without re-opening it (line 44).

    Regards,
    Jim
    Okay, so I changed it to this:

    Java Code:
    File humidFile2 = new File("KeyWestHumid.txt");
    		Scanner inHumid2 = new Scanner(humidFile2);
    		while (inHumid2.hasNext()) {
    			count += 1;
    			txtDataOne = inHumid.next();
    			for (int x = 0; x < 2; x++) {
    				humidArray[count] = Integer.parseInt(txtDataOne);// to place the
    																	// value of
    																	// number
    			}
    
    		}
    		inHumid.close();
    but it still does the same thing :/ it's not getting any values from the text file...it didn't print anyting out

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

    Default Re: How to add values to an array?

    Use some System.out.println() calls to print out the values of things as it goes through a run.
    Things like "count = " + count, and the txtDataOne value, and countNum...everything in fact.
    That should give you an idea of where the code is going, because at the moment you are flying blind.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to add values to an array?

    Quote Originally Posted by Tolls View Post
    Use some System.out.println() calls to print out the values of things as it goes through a run.
    Things like "count = " + count, and the txtDataOne value, and countNum...everything in fact.
    That should give you an idea of where the code is going, because at the moment you are flying blind.
    I just did what you said and I don't know how this is possible but when I changed my code to:

    Java Code:
    File humidFile = new File("KeyWestHumid.txt");
    		Scanner inHumid = new Scanner(humidFile);
    		while(inHumid.hasNext()){
    			count+=1;
    			System.out.println(count);
    		}
    It was seriously printing out huge numbers non stop it was like in 300,000's and that doesn't make sense since the text file I'm scanning only has this in it:

    69 67 66 64 66 69 67 67 70 69 69 70

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How to add values to an array?

    What are you doing in the loop which would cause hasNext to eventually return false and exit the loop?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to add values to an array?

    lol o.o; big words...ummm honestly this is how we were taught to scan the file I don't really know, all it should be doing is seeing how many strings are in the text file and count each string

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How to add values to an array?

    I actually missed that the first time I saw your code. You need to read a value. Otherwise, it will loop for ever.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: How to add values to an array?

    Yep.
    The hasNext() simply checks if there is another token (ie line) to read.
    It doesn't actually read the line.
    That's what next() (or nextLine()) is for.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How to add values to an array?

    Ok, if you have the time, and it still isn't working, here is what I want you to do.

    Write a small program that simply counts the number of values you need to process. This is the same as determining the size of the array. Then print out that number.

    Write a small program that simply reads in the values of the file and prints them out.

    Once you do that, they modify the code to simply store the values in an array.
    Then in a separate loop, iterate thru the array and print out the values.

    The point of all this is to break your problem down into small pieces. It makes it easier to verify that they work as intended. Then if problems arise, you will have a better idea of what to focus on.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to add values to an array?

    Quote Originally Posted by jim829 View Post
    Ok, if you have the time, and it still isn't working, here is what I want you to do.

    Write a small program that simply counts the number of values you need to process. This is the same as determining the size of the array. Then print out that number.

    Write a small program that simply reads in the values of the file and prints them out.

    Once you do that, they modify the code to simply store the values in an array.
    Then in a separate loop, iterate thru the array and print out the values.

    The point of all this is to break your problem down into small pieces. It makes it easier to verify that they work as intended. Then if problems arise, you will have a better idea of what to focus on.

    Regards,
    Jim

    I did what you said and made this:

    Java Code:
    package apcsChapter6;
    
    import java.io.File;
    //import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class FileNumberCount {
    
    	/**
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		String token = "";
    		int count = 0;
    		
    		File humidFile = new File("KeyWestHumid.txt");
    		Scanner inHumid = new Scanner(humidFile);
    		while (inHumid.hasNext()) {
    			inHumid.next();
    			count+=1;
    		}
    		System.out.println("The number of words is: " + count);
    		
    		while(inHumid.hasNext()){
    			token = inHumid.nextLine();
    			System.out.println(token);
    		}
    
    	}
    
    }
    But for some reason the last while loop the one that is suppose to be reading the file isn't printing anything out :/

  14. #14
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to add values to an array?

    Okay I fixed the first issue I had where it was reading it weird and stuff and now it prints out the values like you wanted, but I still have no clue how to get it to add that to the array :/

    Java Code:
    package apcsChapter6;
    
    import java.io.File;
    //import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class FileNumberCount {
    
    	/**
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		String token = "";
    		int count = 0;
    		
    		File humidFile = new File("KeyWestHumid.txt");
    		Scanner inHumid = new Scanner(humidFile);
    		while (inHumid.hasNext()) {
    			inHumid.next();
    			count+=1;
    		}inHumid.close();
    		System.out.println("The number of words is: " + count);
    		File humidFileAgain = new File("KeyWestHumid.txt");
    		Scanner inHumidAgain = new Scanner(humidFileAgain);
    		while (inHumidAgain.hasNext()) {
    			token = inHumidAgain.nextLine();
    			System.out.println(token);
    		}
    		inHumidAgain.close();
    
    	}
    
    }

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How to add values to an array?

    Each time you want to read the same file over again you need to close it and then reopen it. Otherwise, the hasNext() method will return false because you already reached the end of file.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to add values to an array?

    Yeah okay o.o but how do I get the values into the array? :/ that's the part I need to know and I don't even have a clue

    Java Code:
    String token = "";
    		int arrayPosition = 0, numberForArray = 0;
    		int[] humidArray = new int[arrayPosition];
    
    		File humidFile1 = new File("KeyWestHumid.txt");
    		Scanner inHumid1 = new Scanner(humidFile1);
    		while (inHumid1.hasNext()) {
    			arrayPosition += 1;
    			token = inHumid1.next();
    			numberForArray = Integer.parseInt(token);
    			humidArray[arrayPosition - 1] = numberForArray;
    		}
    		inHumid1.close();
    
    		for (Integer arrayHumid : humidArray) {
    			System.out.printf("%5s", arrayHumid);
    		}
    I changed the code to this that way every time it goes through the while loop, it'll add another number to the array but it doesn't work. I set it up this way so that it'd start at 0 because the first time it goes through the while loop it'll go to whatever the value of arrayPosition is, however arrays start at 0 so that's why I did it that way but it still isn't working, it just crashes :/

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How to add values to an array?

    Ok, first you are allocating an array of size 0. Look at line 3. It should be allocated to the size of the count you established in the first loop. Also, move your arrayPosition increment as the last statement in the loop. Then delete the -1 from the array index. Finally, change your printf format argument to "%5d%n" so you can see each value on a different line.

    Also, you can reuse the File object and the Scanner object. You don't need to redeclare either one. Just close it and reopen the file each time you want to read it from the beginning.


    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to add values to an array?

    Quote Originally Posted by jim829 View Post
    Ok, first you are allocating an array of size 0. Look at line 3. It should be allocated to the size of the count you established in the first loop. Also, move your arrayPosition increment as the last statement in the loop. Then delete the -1 from the array index. Finally, change your printf format argument to "%5d%n" so you can see each value on a different line.

    Also, you can reuse the File object and the Scanner object. You don't need to redeclare either one. Just close it and reopen the file each time you want to read it from the beginning.


    Regards,
    Jim
    okay I did all that except the first part, where do I move the array then? it needs to have a place where the array size was suppose to be counted o.o

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How to add values to an array?

    OK. Here are the steps.

    1. First loop. Calculates size of array.
    2. Allocate array using just calculated size
    3. Second loop. Read in values and store in array.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Which values would you put into an array?
    By watcher368 in forum New To Java
    Replies: 1
    Last Post: 11-30-2012, 01:39 PM
  2. Values in Array
    By jazob in forum New To Java
    Replies: 2
    Last Post: 12-10-2011, 01:29 AM
  3. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  4. An Array of different integer values
    By lithium002 in forum New To Java
    Replies: 7
    Last Post: 12-04-2009, 05:25 AM
  5. Same values in an array
    By hawaiifiver in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 08:33 PM

Tags for this Thread

Posting Permissions

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