Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java and exception handling

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java and exception handling

    I know I can calculate the sum of squares as such:

    // SumSquares.java: calculate the sum of two squares
    class SumSquares {
    static int sumSquares(int a, int B)/>/> {
    int asquare;
    int bsquare;
    asquare = a * a;
    bsquare = b * b;
    return asquare + bsquare;
    }
    public static void main(String[] args) {
    int x = Integer.parseInt(args[0]);
    int y = Integer.parseInt(args[1]);
    System.out.print("The sum of squares is: ");
    System.out.println(sumSquares(x,y));
    }
    }

    But how can I modify the code so that it inputs a list of integer values in the range of -100 to 100 from the keyboard and computes the sum of the squares input values. And how would I go about using exception handling to ensure that the input values are in range and are legal integers.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java and exception handling

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Please format/indent your code correctly. There is also an error that converted the small 'b' into a 'B' and added stray characters afterwards. I've seen this on another site but not this one, so it looks as though this code was simply copied from another forum and pasted here to get help modifying it.

    Please post your own code.

    For getting input from the keyboard, look into Scanner. You can also Google 'java exception handling' to find tutorials on how to use exceptions in Java.

Similar Threads

  1. Exception Handling
    By LinuxPower in forum Java Theory & Questions
    Replies: 4
    Last Post: September 2nd, 2013, 10:10 PM
  2. need help in exception handling
    By gauravdudeja in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2013, 06:14 AM
  3. I/O AND EXCEPTION HANDLING need help
    By awood in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 3rd, 2013, 04:22 PM
  4. Replies: 2
    Last Post: August 3rd, 2011, 11:13 AM