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 3 of 3

Thread: What is wrong with this method?

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry What is wrong with this method?

    This method is supposed to check if the inputed number is a number and not a letter and then complete the function. However, every time I try it always is saying the input is wrong.

    For example:

    If I type in "5 * 5"

    it says Error Incorrect Input
    Re-input your numbers:

    when it shouldn't do anything. I can't figure it out.

    My Code Is:
         public void multiply() throws IOException {       
            boolean valid = true;
            boolean valid1 = true;
            int i = 0;
            int k = 0;
            String string = input;
            String[] strings = string.split("\\*");
            String string1 = strings[0];
            String string2 = strings[1];
            for (valid = true; i < strings[0].length();) {
                if (strings[0].charAt(i) == '0'
                        || strings[0].charAt(i) == '1'
                        || strings[0].charAt(i) == '2'
                        || strings[0].charAt(i) == '3'
                        || strings[0].charAt(i) == '4'
                        || strings[0].charAt(i) == '5'
                        || strings[0].charAt(i) == '6'
                        || strings[0].charAt(i) == '7'
                        || strings[0].charAt(i) == '8'
                        || strings[0].charAt(i) == '9'
                        || strings[0].charAt(i) == '*') {
                    valid = true;
                } else {
                    valid = false;
                }
                i++;
            }
            for (valid1 = true; k < strings[1].length();) {
                if (strings[1].charAt(k) == '0'
                        || strings[1].charAt(k) == '1'
                        || strings[1].charAt(k) == '2'
                        || strings[1].charAt(k) == '3'
                        || strings[1].charAt(k) == '4'
                        || strings[1].charAt(k) == '5'
                        || strings[1].charAt(k) == '6'
                        || strings[1].charAt(k) == '7'
                        || strings[1].charAt(k) == '8'
                        || strings[1].charAt(k) == '9'
                        || strings[1].charAt(k) == '*') {
                    valid1 = true;
     
                } else {
                    valid1 = false;
                }
                k++;
     
     
                 if (valid1 == true && valid == true) {
                         System.out.println("Multiply"); 
                      //Going to finish
                    } else {
                        System.out.println("Error Incorrect Input");
                        System.out.println("Re-input your numbers:");
                        check();
                    }
            }
        }


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: What is wrong with this method?

    Are you checking the string for spaces?

  3. The Following User Says Thank You to beansnbacon For This Useful Post:

    CrizR (May 9th, 2014)

  4. #3
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: What is wrong with this method?

    Its cause your spaces. You split the first string into "5 ", and go through both characters. Space is not equal to a number so your valid is set to false. Then it goes to the next loop, and sets valid1 to false, then valid1 to true, and checks to see if valid and valid1 are both true.. yet valid is still false.

  5. The Following User Says Thank You to theoriginalanomaly For This Useful Post:

    CrizR (May 9th, 2014)

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 02:33 AM
  3. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  4. can't figure whats wrong with my add method?? help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 05:00 PM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM

Tags for this Thread