1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java programming need help

Discussion in 'Programming & Webmastering' started by GavinVG, May 18, 2010.

  1. GavinVG

    GavinVG New Member

    Joined:
    Apr 14, 2010
    Messages:
    13 (0.01/day)
    Thanks Received:
    1
    I have to write a program that initializes the sum to zero, accepts the inetger from the user, adds the integer to the sum. If the integer is zero the program continues to print the sum of all numbers enterned but if the integer is not zero it goes back to accepting the integer from the user
    Which means i will have to use a do while loop i think. Maybe a while loop i dont know.
    Last edited: May 18, 2010
  2. GavinVG

    GavinVG New Member

    Joined:
    Apr 14, 2010
    Messages:
    13 (0.01/day)
    Thanks Received:
    1
    Ive got so far

    import java.io.IOException;

    public class Sumitup
    {
    int sum =0;
    int user_number;
    String enter_int = new String();
    do
    {
    System.out.println("Enter a number(Enter 0 to quit):");
    temp = GetConsoleString();
    user_number = Integer.parseInt( temp );
    sum = user_number + sum;
    }
    while (user_number !=0);
    System.out.println("sum ofall numbers is " + sum);


    }
    public static String GetConsoleString()
    {
    int noMoreInput = -1;
    char enterKeyHit = '\n';
    int InputChar;
    StringBuffer InputBuffer = new StringBuffer();

    try
    {
    InputChar = System.in.read();

    while( InputChar != noMoreInput )
    {
    if( (char) InputChar != enterKeyHit )
    {
    InputBuffer.append( (char)InputChar );
    }
    else
    {
    InputBuffer.setLength( InputBuffer.length() -1 );
    break;
    }
    InputChar = System.in.read();
    }
    }
    catch( IOException IOX )
    {
    System.err.println( IOX );
    }

    return InputBuffer.toString();
    }
  3. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,319 (6.34/day)
    Thanks Received:
    3,344
    Location:
    IA, USA
    So...what's wrong? It looks like you got the jist of it already.
    Crunching for Team TPU
  4. GavinVG

    GavinVG New Member

    Joined:
    Apr 14, 2010
    Messages:
    13 (0.01/day)
    Thanks Received:
    1
    The program still doesnt work i am trying run it in eclipse and it still doesnt work there are errors in the temp and parse int statements i have this now

    import java.io.IOException;

    public class Sumitup
    {
    public static void main(String args[])
    {
    int sum =0;
    int temp;
    int user_number;
    String enter_int = new String();
    do
    {
    System.out.println("Enter a number(Enter 0 to quit):");
    temp = GetConsoleString();
    user_number = Integer.parseInt( temp );
    sum = user_number + sum;
    }
    while (user_number !=0);
    System.out.println("sum ofall numbers is " + sum);


    }
    public static String GetConsoleString()
    {
    int noMoreInput = -1;
    char enterKeyHit = '\n';
    int InputChar;
    StringBuffer InputBuffer = new StringBuffer();

    try
    {
    InputChar = System.in.read();

    while( InputChar != noMoreInput )
    {
    if( (char) InputChar != enterKeyHit )
    {
    InputBuffer.append( (char)InputChar );
    }
    else
    {
    InputBuffer.setLength( InputBuffer.length() -1 );
    break;
    }
    InputChar = System.in.read();
    }
    }
    catch( IOException IOX )
    {
    System.err.println( IOX );
    }

    return InputBuffer.toString();
    }
    }
  5. GavinVG

    GavinVG New Member

    Joined:
    Apr 14, 2010
    Messages:
    13 (0.01/day)
    Thanks Received:
    1
    Never mind i found out what i did.
  6. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,319 (6.34/day)
    Thanks Received:
    3,344
    Location:
    IA, USA
    Glad you got it sorted. :)
    Crunching for Team TPU
  7. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,382 (4.78/day)
    Thanks Received:
    3,218
    what was wrong with it?

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page