Results 1 to 20 of 20
  1. #1
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Help With Array Parsing

    Hi. I'm still very new to Java Programming, and I have two methods that parse character arrays and returns an int value. Here is the driver and the methods. (Note: I was given the driver):
    Driver:
    PHP Code:
    char[] chars = {'3', '5', '3', '9'};
        System.out.println(MyInteger.parseInt(chars));
    
        String s = "3539";
        System.out.println(MyInteger.parseInt(s));
    Methods
    PHP Code:
    public static int parseInt(char[] chars) { 
            int summation = 0;
            for(int i = 0; i < chars.length; i++){
                summation += chars[i]-'0';
            }
           return summation; 
        }
    
        public static int parseInt(String s) {
            
            int summation = 0;
            int size = s.length();
            for(int i = 0; i < size; i++){
                summation += s.charAt(i) -'0';
            }
           return summation; 
        }
    Both of these return 20. I feel like these are correct, but the website where I'm suppose to submit my assignment says that it is incorrect. While this is just part of the assignment, I feel as if this is where the error is. If this is correct, I can post the rest of the driver/class for futher help.

    Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help With Array Parsing

    What is the program supposed to do?
    Does it take input and produce some output?
    Can you show some input and the output it produces?

    The only problem I see would be there is no test for valid input. The program assumes that the input is valid.

  3. #3
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Help With Array Parsing

    It's a program purely for schoolwork practice. Here's the rest of the driver:
    PHP Code:
    MyInteger n1 = new MyInteger(5);
        System.out.println("n1 is even? " + n1.isEven());
        System.out.println("n1 is prime? " + n1.isPrime());
        System.out.println("15 is prime? " + MyInteger.isPrime(15));
    
        char[] chars = {'3', '5', '3', '9'};
        System.out.println(MyInteger.parseInt(chars));
    
        String s = "3539";
        System.out.println(MyInteger.parseInt(s));
        
        MyInteger n2 = new MyInteger(24);
        System.out.println("n2 is odd? " + n2.isOdd());
        System.out.println("45 is odd? " + MyInteger.isOdd(45));
        System.out.println("n1 is equal to n2? " + n1.equals(n2));
        System.out.println("n1 is equal to 5? " + n1.equals(5));
    Here's the output it gives me:
    n1 is even? false
    n1 is prime? true
    15 is prime? false
    20
    20
    n2 is odd? false
    45 is odd? true
    n1 is equal to n2? false
    n1 is equal to 5? true

    Unless I've made a mental error, I don't see where anything is wrong. Which is why I think it has something to do with the parsing.

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Help With Array Parsing

    Because summation actually does casting to int
    summation += s.charAt(i) -'0' =
    (int)'3' - (int)'0' = (int)51 - (int)48 = (int)3
    etc

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help With Array Parsing

    Sorry, I have no idea what criteria the site you submit your programs to has for evaluating them.
    As I said, your code does not check for invalid input. Is that part of the test?
    What about negative numbers?

  6. #6
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Help With Array Parsing

    Diamond, that's what I thought it was doing, but my question is "Is 20 the incorrect answer?"

    Norm, honestly, I do not know the criteria the site uses to check the programs, this is the first assignment the university has ever used for it, I'm essentially in a test class. But I don't think negative numbers are to be worried about because the array and string are hard-coded, not based off of any sort of input.

  7. #7
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Help With Array Parsing

    Quote Originally Posted by Conan View Post
    Diamond, that's what I thought it was doing, but my question is "Is 20 the incorrect answer?"
    As I know, 3 + 5 + 3 + 9 = 20, so how can it be incorrect?

  8. #8
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Help With Array Parsing

    That's exactly what my thought was. But somewhere along the lines, according to this website, my code is wrong. Even though, I've ran through the code and I can't find an error.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help With Array Parsing

    How is the code you have written tested? Do you paste the source into a text field?

    Can you post all of the code that you submit to the testing script?

    Can there be a bug in the tester?

  10. #10
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Help With Array Parsing

    I pasted the driver and my class into a text box for it to be tested.

    Here is my entire class (the driver is posted above):

    PHP Code:
    public class MyInteger {
    
        private int value;
        // Instance variable
    
        public MyInteger(int x) {
            value = x;
        }
        // Constructor
        public int getValue() {
            return value;
        }
        // Finds and returns the integer
        
        public boolean isEven() {
            if (value % 2 == 0) {
                return true;
            } else {
                return false;
            }
    
        }
        //Finds if the integer is even    
        
        public boolean isOdd() {
            if (value % 2 == 1) {
                return true;
            } else {
                return false;
            }
    
        }
        //Finds if the integer is odd
    
        public boolean isPrime() {
    
            for (int i = 2; i < value; i++) {
                if (value % i == 0) {
                    return false;
                }
            }
            return true;
        }
        //Finds if the integer is prime
    
        public static boolean isEven(int x) {
            if (x % 2 == 0) {
                return true;
            } else 
                return false;
            
        }
        //Finds if the integer is even 
    
        public static boolean isOdd(int x) {
            if (x % 2 == 1) {
                return true;
            } else {
                return false;
            }
        }
        //Finds if the integer is odd
    
        public static boolean isPrime(int x) {
    
            for (int i = 2; i < x; i++) {
                if (x % i == 0) {
                    return false;
                }
            }
            return true;
        }
        //Finds if the integer is prime
    
        public static boolean IsEven(MyInteger integer) {
    
            if (integer.value % 2 == 0) {
                return true;
            } else {
                return false;
            }
    
        }
        //Finds if the integer is even 
    
        public static boolean IsOdd(MyInteger integer) {
    
            if (integer.value % 2 == 1) {
                return true;
            } else {
                return false;
            }
    
        }
        //Finds if the integer is odd
    
        public static boolean IsPrime(MyInteger integer) {
            
            for (int i = 2; i < integer.value; i++) {
                if (integer.value % i == 0) {
                    return false;
                }
            }
            return true;
        }
        //Finds if the integer is prime   
    
        public boolean equals(int x) {
    
            if (x == value) {
                return true;
            } else {
                return false;
            }
    
        }
        //Finds out if the two integers are the same 
    
        public boolean equals(MyInteger integer) {
    
            if ( integer.equals(this.value) ) {
                return true;
            } else {
                return false;
            }
            
        }
        //Finds out if the two integers are the same
    
        public static int parseInt(char[] chars) { 
            int summation = 0;
            for(int i = 0; i < chars.length; i++){
                summation += chars[i]-'0';
            }
           return summation; 
        }
    
        public static int parseInt(String s) {
            
            int summation = 0;
            int size = s.length();
            for(int i = 0; i < size; i++){
                summation += s.charAt(i) -'0';
            }
           return summation; 
        }
            
        }
    As for a bug in the tester, I doubt it because there was another exercise along with this one that I had to sumbit the exact same way, and it worked fine.

  11. #11
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Help With Array Parsing

    Why this?
    line15: public boolean isEven()
    line 46: public static boolean IsEven(MyInteger integer)

    It is enough to have one static method, no need for instance method isEven.
    Same with isPrime method.
    Last edited by diamonddragon; 01-23-2012 at 11:57 PM.

  12. #12
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Help With Array Parsing

    Yeah, I know that. But every method that I have is because the book told me to have them. I did notice that it was redundant, but I figured it was the book wanting us to make a habit of putting in no-arg methods.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help With Array Parsing

    (the driver is posted above):
    Where? I see bits and pieces of code but no class definition.

    Is it possible to compile and execute the code without the tester site?

  14. #14
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Help With Array Parsing

    The driver and output are both in the third post on this thread. And yes, I wrote this in Netbeans and it worked fine. The site gives me the exact same output, but it says it is an incorrect answer.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help With Array Parsing

    The code in post#3 would not compile without a lot of added code.

  16. #16
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Help With Array Parsing

    It does, I just left out the package (Exercise 10_03). And the code in post 3 is just under the main (public static void main(String[] args)) class.

  17. #17
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Help With Array Parsing

    This:

    char[] chars = {'3', '5', '3', '9'};
    System.out.println(MyInteger.parseInt(chars));

    should return 3539, not 20, what do You think?

  18. #18
    Conan is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Help With Array Parsing

    That was my whole point in posting. I wasn't sure on what it should return, or how to fix it.

  19. #19
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Help With Array Parsing

    Quote Originally Posted by Conan View Post
    That was my whole point in posting. I wasn't sure on what it should return, or how to fix it.
    Maybe something like:

    Java Code:
    }
      summation += s.charAt(i) -'0';
      summation *= 10;
    }
    summation /= 10;

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help With Array Parsing

    With out a definition of what the code is supposed to do, it is just luck that will allow you to write code that produces the "desired" results.
    What is the parseInt() method supposed to return?

Similar Threads

  1. parsing chars for an array
    By Ciaran.moore.clane in forum New To Java
    Replies: 5
    Last Post: 12-15-2011, 01:07 PM
  2. How To Parsing XMI?
    By rauff in forum XML
    Replies: 11
    Last Post: 07-07-2010, 04:16 AM
  3. Parsing XML
    By virvalid in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2009, 12:40 PM
  4. Parsing
    By Matt Sakko in forum New To Java
    Replies: 14
    Last Post: 03-21-2009, 04:49 PM
  5. xml parsing help
    By tankhardrive in forum XML
    Replies: 2
    Last Post: 02-11-2009, 11:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •