By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

Tallying numbers in an array???

P: 10
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:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. public class SunMoon {
  4.  
  5.  
  6.     public static void main(String[] args) throws FileNotFoundException {
  7.         // TODO Auto-generated method stub
  8.         File file = new File("Portland.txt");
  9.         try {
  10.             Scanner console = new Scanner(file);
  11.             while (console.hasNextLine()) {
  12.                 Scanner lineScanner = new Scanner(console.nextLine());
  13.                 lineScanner.useDelimiter("[/,\\s]+");
  14.                 String year = lineScanner.next();
  15.                 int[] years=new int[year.length()];
  16.                 for(int i=0;i<year.length();i++){
  17.                     years[i]=Integer.parseInt(year);
  18.                     break;
  19.                 }
  20.                 String month=lineScanner.next();
  21.                 int[] months=new int[month.length()];
  22.                 for(int i=0;i<month.length();i++){
  23.                     months[i]=Integer.parseInt(month);
  24.                 }
  25.                 String day=lineScanner.next();
  26.                 int[] days=new int[day.length()];
  27.                 for(int i=0;i<day.length();i++){
  28.                     days[i]=Integer.parseInt(day);
  29.                 }
  30.                 String tmin=lineScanner.next();
  31.                 int[] tMin=new int[tmin.length()];
  32.                 for(int i=0;i<tmin.length();i++){
  33.                     tMin[i]=Integer.parseInt(tmin);
  34.                 }
  35.                 String tmax=lineScanner.next();
  36.                 int[] tMax=new int[tmax.length()];
  37.                 for(int i=0;i<tmax.length();i++){
  38.                     tMax[i]=Integer.parseInt(tmax);
  39.                 }
  40.                 String prcp=lineScanner.next();
  41.                 int[] pRCP=new int[prcp.length()];
  42.                 for(int i=0;i<prcp.length();i++){
  43.                     pRCP[i]=Integer.parseInt(prcp);
  44.                 }
  45.                 String snow=lineScanner.next();
  46.                 int[] sNOW=new int[snow.length()];
  47.                 for(int i=0;i<snow.length();i++){
  48.                     sNOW[i]=Integer.parseInt(snow);
  49.                 }
  50.                 String snwd=lineScanner.next();
  51.                 int[] sNWD=new int[snwd.length()];
  52.                 for(int i=0;i<snwd.length();i++){
  53.                     sNWD[i]=Integer.parseInt(snwd);
  54.                 }
  55.             }
  56.             console.close();
  57.         }
  58.         catch (FileNotFoundException e) {
  59.             e.printStackTrace();
  60.         }
  61.     }
  62.     public static void skiableDays(int[] sNWD) {
  63.         int skiable=0;
  64.         for (int i = 0; i < sNWD.length; i++) { 
  65.             if (sNWD[i] >= 4){
  66.                 skiable++;
  67.             }
  68.         }System.out.println("There were "+skiable+" skiable days.");
  69.  
  70.     }
  71.  
  72. }
  73.  
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.
Apr 24 '12 #1

✓ answered by Rabbit

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.

Share this Question
Share on Google+
5 Replies


Rabbit
Expert Mod 10K+
P: 12,421
You never call your skiableDays function.
Apr 24 '12 #2

P: 10
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:
Expand|Select|Wrap|Line Numbers
  1. String snwd=lineScanner.next();
  2.                 int[] sNWD=new int[snwd.length()];
  3.                 for(int i=0;i<snwd.length();i++){
  4.                     sNWD[i]=Integer.parseInt(snwd);
  5.                 }System.out.println("There were "+skiableDays(sNWD)+" skiable days.");
  6.             }
  7.             console.close();
  8.         }
  9.         catch (FileNotFoundException e) {
  10.             e.printStackTrace();
  11.         }
  12.     }
  13.  
I did try it in several other places. One got me the same thing the rest didn't work.
Apr 30 '12 #3

Rabbit
Expert Mod 10K+
P: 12,421
That's because in your original function, you just add one to the skiable variable instead of adding the value.
Apr 30 '12 #4

P: 10
I'm still missing something here... I changed the skiableDays() method to this:
Expand|Select|Wrap|Line Numbers
  1. public static void skiableDays(int[] sNWD) {
  2.         int skiable=0;
  3.         int i=0;
  4.          skiable+=sNWD[i];
  5.         if (skiable >= 4){
  6.         System.out.println("There were "+skiable+" skiable days.");
  7.         }
  8.     }
  9.  
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
Expand|Select|Wrap|Line Numbers
  1. 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.
Apr 30 '12 #5

Rabbit
Expert Mod 10K+
P: 12,421
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.
Apr 30 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.