Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default Finding totals - HELP!!

    I am having a problem with finding the total of an integer array given certain conditions of the array.
    I have a program that writes integers to a text file and adds new values to the same text file each time it is run. For example, the first run of the program leads to a text file being produced with one integer value in. The next program run adds another integer to the end of the same text file and so on. What I want to do is write a piece of code that determines when the total of the integers in the text file reaches a certain value, say 10. However, the total must come from consecutive values in the file. So, for example, if the program generates the numbers 1,2,2,1,3,1 then a total of 10 would be flagged. However, if the program generates the numbers 1,0,2,2,1,3,1 then it wouldn't flag anything as the total of consecutive values is only 9. I thought about maybe trying to incorporate a counter that increases by 1 if it comes across a 1, increases by 2 if it comes across a 2 etc., and resets back to zero if it comes across a zero but I'm not sure how to go about doing that.
    Does anybody have any ideas of the best way to tackle this??

    Many thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mikeg View Post
    I am having a problem with finding the total of an integer array given certain conditions of the array.
    I have a program that writes integers to a text file and adds new values to the same text file each time it is run. For example, the first run of the program leads to a text file being produced with one integer value in. The next program run adds another integer to the end of the same text file and so on. What I want to do is write a piece of code that determines when the total of the integers in the text file reaches a certain value, say 10. However, the total must come from consecutive values in the file. So, for example, if the program generates the numbers 1,2,2,1,3,1 then a total of 10 would be flagged. However, if the program generates the numbers 1,0,2,2,1,3,1 then it wouldn't flag anything as the total of consecutive values is only 9. I thought about maybe trying to incorporate a counter that increases by 1 if it comes across a 1, increases by 2 if it comes across a 2 etc., and resets back to zero if it comes across a zero but I'm not sure how to go about doing that.
    Does anybody have any ideas of the best way to tackle this??

    Many thanks in advance
    Please define your definition of 'consecutive values'. I don't understand what they are from your example.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    OK let me try and explain...
    The values in the text file represent scores. I want the program to flag when a total score of 10 is reached. So for example, if the values in the text file were 2,2,2,2,2 the the program would flag up a total score of 10 has been reached. If however, the values were 2,2,2,0,2,2 then it wouldn't flag because, although the total is actually 10, it is not from numbers in a continuous run. So for this set of values (2,2,2,0,2,2) the running total is 6 and then 4. In other words, if the value is zero then the running total goes back to zero. Hope that makes a bit more sense and many thanks for your help.

    Mike

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mikeg View Post
    OK let me try and explain...
    The values in the text file represent scores. I want the program to flag when a total score of 10 is reached. So for example, if the values in the text file were 2,2,2,2,2 the the program would flag up a total score of 10 has been reached. If however, the values were 2,2,2,0,2,2 then it wouldn't flag because, although the total is actually 10, it is not from numbers in a continuous run. So for this set of values (2,2,2,0,2,2) the running total is 6 and then 4. In other words, if the value is zero then the running total goes back to zero. Hope that makes a bit more sense and many thanks for your help.
    Ah, got it; thanks for the explanation. So for every number read from the file you have a value 'total' containing the total value of the sequence of numbers and you have a value 'number' containing the current value. For every number read you have to run this:

    Java Code:
    if (number == 0)
       total= 0;
    else {
       total+= number;
       if (total >= 10) 
          flag_it();
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Ah OK that makes sense now. Have been trying to figure that out for days so thank you very much, really appreciate it! I'll give it a go.

    Thanks again

    Mike

  6. #6
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    .................
    Last edited by mikeg; 11-19-2010 at 12:05 PM.

  7. #7
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Sorry not sure what happened there....

    I have written the code below but it doesn't seem to work. It just returns a total of 0 everytime? Is there anything wrong or missing here?

    Java Code:
    int Total = 0;
    int number;
    number = Scores[rows];
    for(rows=0; rows<Scores.length; rows++){
    if(number==0)
    Total=0;
    else{
    Total = Total + number;
    		}
    			
    	}

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mikeg View Post
    Sorry not sure what happened there....

    I have written the code below but it doesn't seem to work. It just returns a total of 0 everytime? Is there anything wrong or missing here?

    Java Code:
    int Total = 0;
    int number;
    number = Scores[rows];
    for(rows=0; rows<Scores.length; rows++){
    if(number==0)
    Total=0;
    else{
    Total = Total + number;
    		}
    			
    	}
    What is the length of the Scores array? What is the value or 'rows' before the loop has started? How come your indentation is so messed up? Don't you have to update the 'number' variable in each pass of the loop?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Sorry, I should have said, that was only part of my code. Everything else is working fine apart from the totalling bit. The old code I was using is shown below and worked fine but this obviously just totalled the whole array.


    Java Code:
    int Total = 0;
    for(rows=0; rows<Scores.length; rows++){
    Total = Total + Scores[rows];}
    		
    		
    		
    System.out.println("Total is "+Total);
    I then changed it to this but now I just get a total of zero returned everytime.

    Java Code:
    int Total = 0;
    int number;
    number = Scores[rows];
    for(rows=0; rows<Scores.length; rows++){
    if(number==0)
    Total=0;
    else{
    Total = Total + number;
    		}
    			
    	}
    Any ideas?? I am very new to java by the way so sorry if it's completely wrong.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mikeg View Post
    Sorry, I should have said, that was only part of my code. Everything else is working fine apart from the totalling bit. The old code I was using is shown below and worked fine but this obviously just totalled the whole array.


    Java Code:
    int Total = 0;
    for(rows=0; rows<Scores.length; rows++){
    Total = Total + Scores[rows];}
    		
    		
    		
    System.out.println("Total is "+Total);
    I then changed it to this but now I just get a total of zero returned everytime.

    Java Code:
    int Total = 0;
    int number;
    number = Scores[rows];
    for(rows=0; rows<Scores.length; rows++){
    if(number==0)
    Total=0;
    else{
    Total = Total + number;
    		}
    			
    	}
    Any ideas?? I am very new to java by the way so sorry if it's completely wrong.
    Yup, you should update 'number' in the body of your loop because it is supposed to be equal to the current rows array element, i.e. number= Scores[rows] doesn't belong above the loop but at the start of the body of the loop.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Yes, tried that aswell but still returns a total of zero everytime no matter what values are in the text file?

    Java Code:
    int Total = 0;
    int number;
    for(rows=0; rows<Scores.length; rows++){
    number = Scores[rows];
    if(number==0)
    Total=0;
    else{
    Total = Total + number;
    	}
    					
    }

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mikeg View Post
    Yes, tried that aswell but still returns a total of zero everytime no matter what values are in the text file?

    Java Code:
    int Total = 0;
    int number;
    for(rows=0; rows<Scores.length; rows++){
    number = Scores[rows];
    if(number==0)
    Total=0;
    else{
    Total = Total + number;
    	}
    					
    }
    That code snippet is correct; print out the content (each element) of your array and see what's in it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Hi, yeah checked that and the elements in the array are as per the text file?

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mikeg View Post
    Hi, yeah checked that and the elements in the array are as per the text file?
    You have to show us a bit more of the (relevant) code; now we can only guess what's happening. The little code snippet in your previous reply is definitely correct.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    This is the code that reads in the text file of scores and calculates the total. Maybe an error somewhere in this part? Thank you for this!!


    Java Code:
    int [] Scores = new int [20000];
      	  File scores = new File("Scores.txt");
    		int rows = 0;
    		
    		BufferedReader bufRdrs  = new BufferedReader(new FileReader(scores));
    		String lines = null;
    		
    	 
    		//read each line of text file
    	while((lines = bufRdrs.readLine()) != null && rows < Scores.length)
    		{	
    		StringTokenizer sts = new StringTokenizer(lines,",");
    		while (sts.hasMoreTokens())
    				{
    			//get next token and store it in the array
    		Scores[rows] = Integer.parseInt(sts.nextToken());
    			
    			
    		}
    		
    		rows++;
    		
    		}
    		
    		
    		int Total = 0;
    		int number;
    		for(rows=0; rows<Scores.length; rows++){
    		number = Scores[rows];
    		if(number==0)
    		Total=0;
    		else{
    		Total = Total + number;
    			}
    							
    		}

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mikeg View Post
    This is the code that reads in the text file of scores and calculates the total. Maybe an error somewhere in this part? Thank you for this!!
    If your file has less than 20000 numbers in it the last elements of your array will still be 0 (no value is assigned to it) and your Total variable will always end up being 0 as well.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Ah ok, makes sense. Is there any way around that? Obviously I can't set the size of the array then. Can I have a dynamic array or is there any way I can get it to ignore any null values?

    thanks again

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mikeg View Post
    Ah ok, makes sense. Is there any way around that? Obviously I can't set the size of the array then. Can I have a dynamic array or is there any way I can get it to ignore any null values?

    thanks again
    You already keep track of the numbers actually read: the row variable equals the total numbers read. In your second loop use the 'rows' variable instead of Scores.length and use another variable as the loop counter. That should do it.

    kind regards,

    Jos

    ps. Alternatively I'd use an ArrayList<Integer> and don't worry about it anymore.
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    OK thank you very much for that. I'll have a go!

    Mike

  20. #20
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    All sorted!! Just added a counter in to the first loop and then changed rows<Scores.length to rows<counter and it works great!! Very pleased!! I've also written a piece of code the writes a zero to the text file when a total of 10 is reached. This then resets the total back to zero.
    Thank you once again for all your help. Have a good weekend!!

    Mike

Page 1 of 2 12 LastLast

Similar Threads

  1. Running totals in java. HELP!
    By mikeg in forum New To Java
    Replies: 8
    Last Post: 11-19-2010, 09:29 AM
  2. finding max value
    By rocky86 in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 01:37 AM
  3. Finding the largest value.
    By Blacky777 in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 08:52 PM
  4. Java Loops - accumulating totals
    By iv3java in forum New To Java
    Replies: 6
    Last Post: 12-15-2009, 05:39 AM
  5. Finding where to draw
    By flok in forum AWT / Swing
    Replies: 4
    Last Post: 11-24-2009, 05:55 PM

Posting Permissions

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