Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Digit sum program have code

Thread view: 
kauloha@comcast.net - 17 Feb 2005 02:30 GMT
Hi everyone, this is my first program in Java, it prints out "Enter any

positive integer: "null" is not numeric

Then it gives me the code of nine, which I realize comes from the
Systme.exit(9) that I used, I don't know how to make it wait for the
input then actually get the input to the variable and do the
calculation.

Thanks for any help or pointers.

package javaapplication1;

import java.io.*;
/**
*
* @author Administrator
*/
public class Main{
   int number, sum, temp;

   /** Creates a new instance of Main */

   public Main() {
       int number = 0, sum = 0, temp = 0;
   }

   /**
    * @param args the command line arguments
    */

   public void commandLine(){
       BufferedReader in = new BufferedReader(
               new InputStreamReader(System.in));
       String s1 = null;
       int integer = 0;
       try
       {
           System.out.print("Enter any positive integer : ");
           s1 = in.readLine();
           integer = Integer.parseInt(s1);
       }
       catch(IOException ioex)
       {
           System.out.println("Input error");
           System.exit(1);
       }
       catch(NumberFormatException nfex)
       {
           System.out.println("\"" + nfex.getMessage() +
               "\" is not numeric");
           System.exit(9);
       }

       System.out.println("Thanks for the number, " + s1);
       Sum(integer, temp, sum);
   }

   public void Sum(int number, int temp, int sum){
       do{
           temp = number % 10;
           sum = sum + temp;
           number = number / 10;
       }
       while(number > 0);
       System.out.print("Those digits added = " + sum);
       System.exit(1);
   }

   public static void main(String[] args) {
       Main test;
       test = new Main();
       test.commandLine();

   }

- Hide quoted text -
- Show quoted text -

}

Reply
SMC - 17 Feb 2005 05:02 GMT
Must be the beginning of another semester... :-)

Works for me. You running this on a command line or using some
IDE/Builder/GUI app?

On Thu, 17 Feb 2005 13:30:23 +1100, kauloha wrote:

> package javaapplication1;
>
[quoted text clipped - 65 lines]
>
> Reply


Signature

Sean

I can only please one person per day. Today is not your day. Tomorrow is
not looking good either.

kauloha@comcast.net - 17 Feb 2005 05:15 GMT
Thanks for the quick response, yeah a start of a semester for me, I
wrote this in netbeans, apparently I'm not sure how to make it run via
command line.
Daniel Tahin - 17 Feb 2005 09:34 GMT
Hi!

Would you like to get the 1. input, 2. do the calculation, 3. print
something out, and then begin with 1.?
For this i modified your code a bit:

package javaapplication1;

import java.io.*;
/**
 *
 * @author Administrator
 */
public class Main{
    int number, sum, temp;
    BufferedReader in;
    String s1;

    /** Creates a new instance of Main */

    public Main() {
        int number = 0, sum = 0, temp = 0;
    in = new BufferedReader( new InputStreamReader(System.in) );
    s1 = null;
    }

    /**
     * @param args the command line arguments
     */

    public void commandLine(){
        //BufferedReader in = new BufferedReader(  //define it as a
global variable, so we don't need
                //new InputStreamReader(System.in)); //to initialize it
each time, commandLine() is called.
        //String s1 = null;
        int integer = 0;
        try
        {
            System.out.print("Enter any positive integer : ");
            s1 = in.readLine();
            integer = Integer.parseInt(s1);
        }
        catch(IOException ioex)
        {
            System.out.println("Input error");
            System.exit(1);
        }
        catch(NumberFormatException nfex)
        {
            System.out.println("\"" + nfex.getMessage() +
                "\" is not numeric");
            System.exit(9);
        }

        System.out.println("Thanks for the number, " + s1);
        Sum(integer, temp, sum);
    }

    public void Sum(int number, int temp, int sum){
        do{
            temp = number % 10;
            sum = sum + temp;
            number = number / 10;
        }
        while(number > 0);
        System.out.print("Those digits added = " + sum);
        //System.exit(1);  //if you call this, the program will quit
after the first Sum()
    }

    public static void main(String[] args) {
        Main test;
        test = new Main();
        while(true) { //calling commandLine() in an endless loop. With
CTRL+C you can terminate the program!
        test.commandLine();
    }

    }

}

kauloha@comcast.net schrieb:
> Thanks for the quick response, yeah a start of a semester for me, I
> wrote this in netbeans, apparently I'm not sure how to make it run via
> command line.
kauloha@comcast.net - 17 Feb 2005 14:27 GMT
Thanks a lot, I'll try that out,


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.