Tallying numbers in an array???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsduck
    New Member
    • Mar 2012
    • 10

    Tallying numbers in an array???

    I'm writing a program that took a matrix and put each column into its own int[]. Now for the part I'm struggling with. I need to tally up the numbers that are >= 4. Here's my code:
    Code:
    import java.io.*;
    import java.util.*;
    public class SunMoon {
    
    	
    	public static void main(String[] args) throws FileNotFoundException {
    		// TODO Auto-generated method stub
    		File file = new File("Portland.txt");
    		try {
    			Scanner console = new Scanner(file);
    			while (console.hasNextLine()) {
    				Scanner lineScanner = new Scanner(console.nextLine());
    				lineScanner.useDelimiter("[/,\\s]+");
    				String year = lineScanner.next();
    				int[] years=new int[year.length()];
    				for(int i=0;i<year.length();i++){
    					years[i]=Integer.parseInt(year);
    					break;
    				}
    				String month=lineScanner.next();
    				int[] months=new int[month.length()];
    				for(int i=0;i<month.length();i++){
    					months[i]=Integer.parseInt(month);
    				}
    				String day=lineScanner.next();
    				int[] days=new int[day.length()];
    				for(int i=0;i<day.length();i++){
    					days[i]=Integer.parseInt(day);
    				}
    				String tmin=lineScanner.next();
    				int[] tMin=new int[tmin.length()];
    				for(int i=0;i<tmin.length();i++){
    					tMin[i]=Integer.parseInt(tmin);
    				}
    				String tmax=lineScanner.next();
    				int[] tMax=new int[tmax.length()];
    				for(int i=0;i<tmax.length();i++){
    					tMax[i]=Integer.parseInt(tmax);
    				}
    				String prcp=lineScanner.next();
    				int[] pRCP=new int[prcp.length()];
    				for(int i=0;i<prcp.length();i++){
    					pRCP[i]=Integer.parseInt(prcp);
    				}
    				String snow=lineScanner.next();
    				int[] sNOW=new int[snow.length()];
    				for(int i=0;i<snow.length();i++){
    					sNOW[i]=Integer.parseInt(snow);
    				}
    				String snwd=lineScanner.next();
    				int[] sNWD=new int[snwd.length()];
    				for(int i=0;i<snwd.length();i++){
    					sNWD[i]=Integer.parseInt(snwd);
    				}
    			}
    			console.close();
    		}
    		catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    	public static void skiableDays(int[] sNWD) {
    		int skiable=0;
    		for (int i = 0; i < sNWD.length; i++) { 
    			if (sNWD[i] >= 4){
    				skiable++;
    			}
    		}System.out.println("There were "+skiable+" skiable days.");
    		
    	}
    	
    }
    I get nothing for my output. I have been at this chipping away since 1:30 P.M and it is now 1:30 A.M. If someone could take a look and point me in the right direction that would be grand.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You never call your skiableDays function.

    Comment

    • rsduck
      New Member
      • Mar 2012
      • 10

      #3
      Ok so I called my skiableDays() function. The issues is that it is now counting the ints per row instead of adding up the rows with ints greater than or equal to 4.
      Example:
      These are the last 7 lines of the output:
      There were 5 skiable days.
      There were 5 skiable days.
      There were 0 skiable days.
      There were 0 skiable days.
      There were 0 skiable days.
      There were 0 skiable days.
      There were 0 skiable days.

      And these are the last 7 lines of the sNWD:
      00013
      00005
      00001
      00000
      00000
      00000
      00000

      Here is where I added it:
      Code:
      String snwd=lineScanner.next();
      				int[] sNWD=new int[snwd.length()];
      				for(int i=0;i<snwd.length();i++){
      					sNWD[i]=Integer.parseInt(snwd);
      				}System.out.println("There were "+[B][U]skiableDays(sNWD)[/U][/B]+" skiable days.");
      			}
      			console.close();
      		}
      		catch (FileNotFoundException e) {
      			e.printStackTrace();
      		}
      	}
      I did try it in several other places. One got me the same thing the rest didn't work.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That's because in your original function, you just add one to the skiable variable instead of adding the value.

        Comment

        • rsduck
          New Member
          • Mar 2012
          • 10

          #5
          I'm still missing something here... I changed the skiableDays() method to this:
          Code:
          public static void skiableDays(int[] sNWD) {
          		int skiable=0;
          		int i=0;
           		skiable+=sNWD[i];
          		if (skiable >= 4){
          		System.out.println("There were "+skiable+" skiable days.");
          		}
          	}
          This grabs every instance where sNWD[i] is 4 or greater and prints that instance. I just realized that I was trying to add all those values together when what I really need to do is count how many times it occurs. Any ideas? I tried a method from another project that counts the lines in a string array, but I couldn't tweak it to work here. I also tried adding this
          Code:
          for(int j=0;j<=skiable;j++)
          after the if statement and before the print statement to no avail, as well as various other approaches that didn't work and were more like stabs in the dark.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            If you want to count, then revert the code back to what it was when you said it was counting and not summing.

            In post #3, you said it was counting but you wanted summing. Just change the code back to that.

            Comment

            Working...