Simple counter

I am trying to read a number from a dat file, add 1 and then write back to the file.
I have tried it with straight read method and readInt and I either get the wrong numbers returned(read) or null(readInt).
Where am I going wrong?
I have cut and pasted many Forum solutions and tried them but I can't seem to get them working either (usually io or type errors when compiling.)
Please help.
// Number Counter
import java.io.*;
public class Counter{
public static void main(String argv[]){
int nis;
try{   
FileInputStream num= new FileInputStream("dnum.dat");
DataInputStream dn = new DataInputStream(num);
System.out.println(dn.read());
int incnum = dn.read();
     int newnum = incnum + 1;
FileOutputStream numout = new FileOutputStream("dnum.dat");
DataOutputStream dos = new DataOutputStream(numout);
dos.write(newnum);
catch(IOException e){System.out.println(e.getMessage());}
}

Hi there,
1.In your code you are reading a byte to be printed and then another one to be edited. So only the second one is changed.
2. With read yout get an int and not the char you want to see.
3. The "int nis" is not needed.
import java.io.*;
public class Counter
  public static void main(String argv[])
    try
      FileInputStream num= new FileInputStream("dnum.dat");
      DataInputStream dn = new DataInputStream(num);
      int incnum = dn.read();
      System.out.println((char)incnum);
      dn.close();
      int newnum = incnum + 1;
      FileOutputStream numout = new FileOutputStream("dnum.dat");
      DataOutputStream dos = new DataOutputStream(numout);
      dos.write(newnum);
      dos.close();
    catch(IOException e){System.out.println(e.getMessage());}
  } Hope that helped.

Similar Messages

  • Simple Count using Analytic view and Odata

    Hi Experts,
    I have an analytic view based on one table and Odata services on top of it.
    Now i want to include three measures just simple count with group by,
    just for this i ddint want to go for calc view. Please help me how to achieve this using analytic view and Odata script.
    Thanks,
    Devi.

    Hello George,
    I don't have any personalization set on the info space. Also I am using mapped Account in BI to connect to HANA. The confusing part is that it is able to validate the infospace with the input parameters and index it. However query does not return any results. I even tried running the same query which explorer sends to HANA in the SQL editor and there too the same results,the query does not return anything. The model does return data when I do a data preview and if accessed from other tools like AAO.
    Also when I use SSO connection to HANA, indexing of the infospace fails. Where can I see the error log?
    Thanks,

  • Analytic workspace manager - simple count?

    I must be missing something in the Analytic workspace manger as all i want is a simple count of the rows. Its a basic type of calculation.
    I have a number of agreements that have a sum of the amount on a Yearly, QTR, Month, Day. I also need the number of agreements for each part of the hierarchy.
    i.e. (contrived daily example)
    SELECT WORKING_DATE,COUNT(AMOUNT),SUM(AMOUNT)
    FROM AGREEMENTS
    GROUP BY WORKING_DATE
    The amounts work fine and the dimensions are working. Just cant get a COUNT working. I have seen work around hacks; but find it hard to believe that these are needed on a basic function that should be there.
    Cheers
    Chris

    OLAP does not make this as easy as it should be, but COUNT can be calculated along with another aggregation operator, like SUM.
    When you create a cube using AWM and specify SUM as the cube aggregation, you should see something like this in the associated cube XML.
    <ConsistentSolve>
    <![CDATA[SOLVE
      SUM
        MAINTAIN COUNT
         OVER ALL
    )]]>
    </ConsistentSolve>Alternatively you can see the specification in the USER_CUBES view.
    select consistent_solve_spec
    from user_cubes
    where cube_name = '...'If you see the MAINTAIN COUNT keywords, then this says that a COUNT has been calculated alongside the SUM. If it isn't there, you can add it to the XML (as shown above) and recreate the cube. You can see the effect of the keywords in the aggmap generated for the cube. Note the "COUNT YES" statements in the following example.
    DEFINE UNITS_CUBE_SOLVE_AGGMAP AGGMAP LOCKDFN
    AGGMAP
    RELATION this_aw!TIME_AGGRREL(this_aw!UNITS_CUBE_SOLVE_TIME_HVSET) PRECOMPUTE(this_aw!UNITS_CUBE_SOLVE_TIME_PVSET) OPERATOR SUM args  DIVIDEBYZERO YES DECIMALOVERFLOW YES NASKIP YES COUNT YES
    RELATION this_aw!CUSTOMER_AGGRREL(this_aw!UNITS_CUBE_SOLVE_CUSTOMER_HVSET) OPERATOR SUM args  DIVIDEBYZERO YES DECIMALOVERFLOW YES NASKIP YES COUNT YES
    RELATION this_aw!PRODUCT_AGGRREL(this_aw!UNITS_CUBE_SOLVE_PRODUCT_HVSET) OPERATOR SUM args  DIVIDEBYZERO YES DECIMALOVERFLOW YES NASKIP YES COUNT YES
    RELATION this_aw!CHANNEL_AGGRREL(this_aw!UNITS_CUBE_SOLVE_CHANNEL_HVSET) OPERATOR SUM args  DIVIDEBYZERO YES DECIMALOVERFLOW YES NASKIP YES COUNT YES
    AGGINDEX NO
    CACHE SESSION
    ENDIf you have this in place, then you can get the value of COUNT using the AGGCOUNT function in OLAP DML. To add a "count of sales" measure to the UNITS_CUBE in global, for example, I created a calculated measure with this expression.
    QUAL(AGGCOUNT(GLOBAL.GLOBAL!UNITS_CUBE_STORED) GLOBAL.GLOBAL!UNITS_CUBE_MEASURE_DIM 'SALES')

  • Simple count up timer won't stop.

    Hi,
    I made a simple count up timer, it is working just fine but I would like to have the %-sign next to the numbers so it would look as a preorder. But as I add "%" sign to the function, the counting never stops (at 100% any more), and continues counting.
    What am I missing??
    var count:Number = 1;
    var myTimer:Timer = new Timer(10);
    myTimer.addEventListener(TimerEvent.TIMER, countdown);
    myTimer.start();
    function countdown(event:TimerEvent):void{
    //myText_txt.text = String((count)+myTimer.currentCount);
    myText_txt.text = String((count)+myTimer.currentCount)+"%";
    if (uint(myText_txt.text) >= 100) {
            myTimer.removeEventListener(TimerEvent.TIMER, countdown);
    Thanks!

    You should learn to use the trace() function to see why things don't work as expected.   The "%" makes converting it to a uint not what you expect.  Trace uint(myText_txt.text)  and you will see why comparing the uint value of a non-numeric string to a number isn't going to work.  Try the following instead...
    var count:Number = 1;
    var myTimer:Timer = new Timer(10);
    myTimer.addEventListener(TimerEvent.TIMER, countdown);
    myTimer.start();
    function countdown(event:TimerEvent):void{
       myText_txt.text = String((count)+myTimer.currentCount)+"%";
       if (uint(String((count)+myTimer.currentCount)) >= 100) {
            myTimer.removeEventListener(TimerEvent.TIMER, countdown);

  • How do I create a simple counter in Dreamweaver CC?

    Hello there Dreamweaver community,
    I am a student creating my first website and I NEED HELP!
    My site is live and it's based on the correct spelling of grey.
    www.lefthandedart.com
    Within the site I have a "vote" page that allows users to vote "grey" or "gray" (note: currently links to other pages on the site). What I want to do is have a simple hit counter under each spelling that adds one when users vote for that spelling. I'm having a fit trying to figure out how to program this though.
    Please help.
      Thank you,
    Terry B

    You'll need scripts to do this.   Most polling/voting scripts use server-side programming like PHP and a few other files to display the results.
    http://www.gentlesource.com/poll-voting/
    PS.  In the US, the correct spelling is gray. 
    Nancy O.

  • I need a simple counter

    Hi everybody,
    I need a counter so that when ever i press the submit button continuously it should display the number starting from 1, can anybody help me with the simple code

    Not as easy as you'd imagine with applets. You need some way to store the hits, usually through a file on the server. That's not gonna happen in a hurry for 2 reasons -
    - Applets can't read/write files
    - Your web server usually won't let you run programs on their machine (ie, programs that listen for socket connections from applets, then load/read/write/close a file).
    In short, no, there is no simple java solution (that I know of).
    Cheers,
    Radish21

  • Simple counter within Control & Simulation Loop

    Does anyone know a simple way of creating an incremental counter within the Control & Simulation Loop ? It's not possible to have a For loop within the Control & Simulation Loop hence the shift register method is out.

    The "simulation parameters" function on the utilities pallete outputs a "Timestep Index" that is incremented each step. That's the simplest option. Alternatively, you could use a sub VI that executes on major steps and contains a for loop.

  • Simple counter in seconds

    Hi,
    In my code I have a while loop that may or may not take more than x secs to run. Once the while loop has been running for x secs I would to provide a series of commands. Therefore, I need some sort of counter. Will Tick Count help me with this? If so, how? If not, how can I create a counter for this simple task? Thank you.
    Alfredo
    Alfredo
    Solved!
    Go to Solution.

    Yes, Tick Count can be used for this, as well as other mechanisms. For example, you could use the Get Date/Time in Seconds function. Here's asome simple examples:
    Attachments:
    Example_VI_BD.png ‏6 KB

  • A simple count egdes task

    Hello,
    I have a simple little Question. My Problem is that the counter counts to many events if the second counter signal is connect. But first I will tell you what have I done. I create a simple egde count Task with MAX. MAX Tells me to connect my encoder signal to PFI 3. If I do this all is okay. But if i connect the second phase shifted encoder signal to PFI 5 (I need this for a another task) the counter counts two times more egdes as it should be. Is this okay? What have I made wrong? Is there a possibilty to avoid this?.
    Thanks for your help
    Best regards
    Stefan Farmbauer

    Hi Stefan,
    first of all, I need more information about your setup:
    1. What NI device are you using (device number, E-Series or M-Series,...)?
    2. What's the Version of your DAQmx driver (check out My System >> Software in MAX)?
    3. What's the Version of your MAX?
    4. Which counters did you specify for your tasks?
    5. How did you configure your counter tasks (edge count, rising/falling,...)?
    6. Which terminal block (to connect your device to the quadratur encoder) are you using?
    7. Which Pins on the terminal block did you select (did you choose the right ones)?
    Please answer the above questions, so I can be of assistance.
    Bye
    Daniel
    NIG

  • Simple Count Down??

    Hi,
    I'm creating a simple game in java 3d. All I want to do is have the game close after 5 minutes. Is it possible to set a five minute count down once the game has started?
    Thanks,
    Sean

    Yes.
    Create a timestamp when it starts running. Periodically check the current time against the timestamp.

  • Simple Count

    All,
    I know I'm asking a basic question here, but what is the simplest way to get the count of all tables in dba_tables. Now, let me explain before I get ripped on really fast.
    I run dbms_stats every morning, which updates the num_rows in the dba_tables. This is the number of rows in the table at the time dbms_stats were ran. What I'm trying to do is calculated how many rows are inserting into the tables after the dbms_stats run at 7 am each morning. This way I can track the growth of tables by the hour instead of by day using dbms_stats.
    I've come up with a small PL/SQL block, but can't get the select count(*) to work. Would like for someone to take a look and make a recommendation or explain why I'm over looking the simple solution.
    /---CODE---/
    SET ECHO OFF
    SET FEEDBACK OFF
    SET SERVEROUTPUT ON SIZE 1000000
    SET LINESIZE 60
    SET TERM ON
    DECLARE
    CURSOR c_tablename
    IS
    SELECT table_name
    FROM dba_tables
    WHERE owner = 'KSTORM';
    v_tablename VARCHAR2 (30) := NULL;
    v_count NUMBER := NULL;
    BEGIN
    FOR r_tablename IN c_tablename
    LOOP
    v_tablename := r_tablename.table_name;
    SELECT COUNT (*)
    INTO v_count
    FROM v_tablename;
    DBMS_OUTPUT.put_line ( 'v_tablename : '
    || v_tablename
    || ' : count : '
    || v_count
    END LOOP;
    END;
    /---END CODE---/
    Thanks,
    Bobby

    DECLARE
      CURSOR c_tablename IS
        SELECT table_name FROM user_tables WHERE table_name = 'EDI_850_HDR';
      v_count     NUMBER := NULL;
    BEGIN
      FOR r_tablename IN c_tablename
      LOOP
        EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || r_tablename.table_name
          INTO v_count;
        DBMS_OUTPUT.put_line('v_tablename : ' || v_tablename || ' : count : ' ||
                             v_count);
      END LOOP;
    END;

  • Simple Count query and fetching data

    Hello,
    I have a table Emp :
    Name, age, sal, dateUpdated
    A 21 100 6/4/10
    B 21 101 6/4/10
    C 32 101 2/2/2
    D 20 100 3/3/3
    I am trying to count the number of people in the same age group AND fetch that number.
    For ex, I want to count the number of people that were updated today and then fetch their age.
    for ex : something like this : select count(dateUpdated) from Emp where date updated is today
    will only return 2 as result.
    how should my query be if I want to even fetch the individual age along with the count ?
    cheers,

    You can use {noformat}{noformat} tags to preserve your code format. That would help every one to read your post better.                                                                                                                                                                                                                                                           

  • Simple Count Function Help

    I have a really basic question. The following SQL query works for me:
    Select  EnterUserID, Enterdate
    from tblCsEventReminders
    where EnterDate >= Convert(datetime, '2015-04-01')
    I am essentially trying to write a query to count the number of user logins after a certain date, so I need to count 'EnterUserID' but I am having problems getting the count() function to work.  Any help is appreciated.
    Kurt 
    Kurt

    Hello Can you try something like this below:
    Select  COUNT(EnterUserID) AS UserLoginCounts
    from tblCsEventReminders
    where EnterDate >= Convert(datetime, '2015-04-01')
    Good Luck! Please Mark This As Answer if it solved your issue. Please Vote This As Helpful if it helps to solve your issue

  • Simple counter for array, help?

    I need to rewrite the ArrayList method for my class basically. I am working on the add method write now and the loop I have to count the number of integers and pass that in as the array list size isn't working. Here is the code:
    import javax.swing.*;
    import java.util.*;
    public class C {
      public static void main(String[] args) {
        int index = 0;
        String inputString = JOptionPane.showInputDialog(null, "Please enter so integers: ");
        Scanner console = new Scanner(inputString);
        while (console.hasNext() == true) {
          index++;
        System.out.println("Index : " + index);
    }It compiles but never prints the index, I think it is going into an infinite loop somehow.
    This is my first post here, hopefully this is the place to forum! :)

    while (console.hasNext() == true) {
    index++;
    It compiles but never prints the index, I think it is going into an infinite loop somehow. It sure is in an infinite loop. How are you expecting console.hasNext() to ever change in that loop if you're never consuming anything from console?

  • Simple count of points within a radius takes too long

    I have reduced my table to just two columns - an ID and a Geography column. I have a spatial index on the latter and a clustered PK index on the former. There are 10m records in the table. The indexes are not fragmented.
    A query to get a count all points within 1000km of a specified point returns around 7.5m but takes 30+ seconds to execute.
    The query plan shows use of the spatial index with cost 9% then clustered index seek on my primary key taking 83%.
    I have tested STDistance and STBuffer - the latter is slightly faster.
    SELECT COUNT(1) FROM testdoserate2 WHERE ([Location].STDistance('POINT (16.373813 48.230391)')) < 1000000
    declare @referencepoint Geography = 'POINT (16.373813 48.230391)'
    declare @radius Geography = @referencepoint.STBuffer(1000000);
    SELECT count(1) FROM testdoserate2 WITH (nolock, INDEX(IDX_Location))
    WHERE Location.Filter(@radius) = 1
    Is there anything else I can possibly do to speed this up?

    Have a look at this thread, especially the info on custom tessellations and the "optimizing point queries" whitepaper:
    https://social.technet.microsoft.com/Forums/sqlserver/en-US/3bbdc511-2a08-4075-b989-d98b70d0bedd/sql-server-express-performance-limitations-with-ogc-methods-on-geometry-instances?forum=sqlspatial
    In addition: If your query returns a lot of rows (or a large count of them) and/or if your query and your spatial index doesn't eliminate most of the candidate rows, you can have perf problems. 1000km radius does sound like it could produce a lot of
    rows, which cause a lot a seeks on the base table in your query plan (capture the *actual* plan and look at the "Clustered Index Seek" iterator right above-right of the "Filter" iterator). 
    Look into tuning the parameters of your spatial index using sp_help_spatial_geography_index. You're looking for largest Percentage/Number_Of_Rows_Selected_By_Primary_Filter and  Pecentage/Number_Of_Rows_Selected_By_Secondary_Filter, which the default
    spatial index parameters don't always give you.
    Hope this helps, Bob

Maybe you are looking for