# Java programming need help



## GavinVG (May 18, 2010)

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.


----------



## GavinVG (May 18, 2010)

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();
}


----------



## FordGT90Concept (May 18, 2010)

So...what's wrong?  It looks like you got the jist of it already.


----------



## GavinVG (May 18, 2010)

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();
}
}


----------



## GavinVG (May 18, 2010)

Never mind i found out what i did.


----------



## FordGT90Concept (May 18, 2010)

Glad you got it sorted.


----------



## Easy Rhino (May 19, 2010)

GavinVG said:


> Never mind i found out what i did.



what was wrong with it?


----------

