473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Tallying numbers in an array???

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.

5 3695
Rabbit
12,516 Expert Mod 8TB
You never call your skiableDays function.
Apr 24 '12 #2
rsduck
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
12,516 Expert Mod 8TB
That's because in your original function, you just add one to the skiable variable instead of adding the value.
Apr 30 '12 #4
rsduck
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
12,516 Expert Mod 8TB
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

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

Similar topics

3
by: Paul | last post by:
The following code: <?php header("Content-Type: text/plain"); $numbers = array(3.714, 0.7142857142857143, 9.667, 6.333, 7, 6, 8, 2, 3, -2, 0, -6.6, 2.25, 4.333, -8, 3, 3.141592654, 5.5, 6.5,...
10
by: Ole | last post by:
Goodday everybody, i want to create an array that represents data, that has to be transferred to a pdf doc ( iTextSharp ). The problem is, that i seem too loose my faith. Namely, ( i have...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
11
by: Schraalhans Keukenmeester | last post by:
I have a substantial random stream of (numerical, long int) data (coming from several SCADA sources) that needs to be passed thru a function that tallies the occurrences of several different bit...
12
by: Steve | last post by:
here's a quirk i can't seem to handle, just hack. since call-time by-reference is depreciated and i don't want to enable it in the php.ini, i'm kind of stuck when i want to pass userdata as an...
1
by: Nightfarer | last post by:
Hello. I have a big trouble using System.Array class (it's the first time I use it) for a software I'm developing. I have a form with a textbox(numbers) and one button (done). Once the number...
2
by: David TG | last post by:
Hiya! I would like to take a partial slice of a fairly complex array. The source looks something like array ( ==array ( ==NN
2
by: tuananh87vn | last post by:
Hi ! can anyone help me with the following topic: Find All Duplicates in a List of Numbers - Array implementation - -InitializeTree() -AddNode() -Add into...
8
by: jabernet | last post by:
Is it possible in the new DB2 9.5 to use the content of an array in an IN expression? Say we have the ddl: CREATE TYPE Numbers AS Integer ARRAY; CREATE tab1 AS ( id integer, numbers...
1
by: niner | last post by:
I have been working on the following code for about 6 hours now. I am attempting to write a program in Java that finds the highest, lowest, sum, and mean of an array. The program should also end...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.