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: Comparing using String.valueOf() method

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Comparing using String.valueOf() method

    I'm working on a program where I need to check if a number is a multiple of 5.
    For my approach, I made a method that converts the int number to an array of digits and is supposed to compare the string value of the last digits of the array with 5 or 0. If it is equal, it is a multiple of 5. But doing so, do not work? Is there an issue with my code or is there an explanation for that?


    Here is the method I made :
    // check if number is multiple of five
        public static boolean isMultipleFive(int number) {
            boolean check = false;
     
            // convert number to an array of digits
            String temp = Integer.toString(number);
            int[] newGuess = new int[temp.length()];
            for (int i = 0; i < temp.length(); i++) {
                newGuess[i] = temp.charAt(i) - '0';
            }
     
            // System.out.println(Arrays.toString(newGuess)); // return [5, 0]
            // System.out.println(newGuess[newGuess.length - 0]); // return 0
     
            if (String.valueOf(newGuess[newGuess.length - 1]) == "5"
                    || String.valueOf(newGuess[newGuess.length - 1]) == String.valueOf(0)) {
                check = true;
            }
     
            return check;
        }

    When I test it with the following line (within the driver code) :

    System.out.println(isMultipleFive(50)); // return false, why?

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Comparing using String.valueOf() method

        public static boolean isMultipleFive(int number) {
            String temp = ""+number;
            char c = temp.charAt(temp.length()-1);
            return c == '5' || c == '0';
        }


    --- Update ---

    you cannot compare 2 strings with ==.
    use .equals
        public static boolean isMultipleFive2(int number) {
            boolean check = false;
            String temp = Integer.toString(number);
            int[] newGuess = new int[temp.length()];
            for (int i = 0; i < temp.length(); i++) {
                newGuess[i] = temp.charAt(i) - '0';
            }
            if (String.valueOf(newGuess[newGuess.length - 1]).equals("5")
                    || String.valueOf(newGuess[newGuess.length - 1]).equals(String.valueOf(0))) {
                check = true;
            }
     
            return check;
        }

Similar Threads

  1. comparing string but the '/0' keeps being counted
    By jocdrew21 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2014, 04:12 PM
  2. Java hangman help with comparing array to String
    By tonynsx in forum What's Wrong With My Code?
    Replies: 24
    Last Post: October 2nd, 2013, 04:50 PM
  3. Help with Casting Vectors and String.valueOf()
    By JSeol14 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 17th, 2013, 04:33 PM
  4. Double.valueof wont work for formated string
    By sstavrou in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 8th, 2013, 01:35 PM
  5. error when comparing 2 string objects
    By amr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 07:36 PM

Tags for this Thread