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

Thread: The operator / is undefined for the argument type(s)

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default The operator / is undefined for the argument type(s)

    Question (school):
    Write a program that prompts for two numbers, then divides the first by the second. Write the answer in the form "<a> divided by <b> is <answer>."

    My Code:
    import java.io.*;  //this needs to be at the top of your program, right below the “packages”
    /**
       * @author: My Name
       * @date: 2013-09-27
       * @filename: ICS3UMyNameJavaAssignment2d.java
       * @description: Asks the user for 2 numbers than divides the first number by the second.
       */
    public class ICS3UMyNameJavaAssignment2d
    {
      public static void main (String[] args) throws Exception {
        {
          BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
          String f; //creates a string variable to get first number from user
          double first = 0.0; //double first needed later
          System.out.print("Enter the first number: ");  //gets first number from user
          f = buffer.readLine(); //reads a line from the console
          first = Double.parseDouble(f);//gets the double value from string variable f
          System.out.println(f + " will be divided by: "); //states string variable f
     
          String s; //creates a string variable to get time (travelled) from user
          double second = 0.0; //double second
          System.out.print("Enter the second number: "); //gets the second number from user
          s = buffer.readLine(); //reads a line from the console
          second = Double.parseDouble(s);//gets the double value from string variable s
          System.out.println("The quotient of " + f + " and " + s + " is " + f/s); //output
        }
      }
    }

    Error:
    1 error found:
    [B]Error: The operator / is undefined for the argument type(s) java.lang.String, java.lang.String[/B]


  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: The operator / is undefined for the argument type(s)

    Place 'f/s' in parentheses, ( f / s ).

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: The operator / is undefined for the argument type(s)

    Error:
    Error: The operator / is undefined for the argument type(s) java.lang.String, java.lang.String

    Is the new error after adding that.

    System.out.println("The quotient of " + f + " and " + s +  " is " + (f / s)); //output


    EDIT:
    I solved it I was using the strings to divide the quotient I had to use the double (first and second not f and s)

    import java.io.*;  //this needs to be at the top of your program, right below the “packages”
    /**
       * @author: My Name
       * @date: 2013-09-27
       * @filename: ICS3UMyNameJavaAssignment2d.java
       * @description: Asks the user for 2 numbers than divides the first number by the second.
       */
    public class ICS3UMyNameJavaAssignment2d
    {
      public static void main (String[] args) throws Exception {
        {
          BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
          String f; //creates a string variable to get first number from user
          double first = 0.0; //double first needed later
          System.out.print("Enter the first number: ");  //gets first number from user
          f = buffer.readLine(); //reads a line from the console
          first = Double.parseDouble(f);//gets the double value from string variable f
          System.out.println(f + " will be divided by: "); //states string variable f
     
          String s; //creates a string variable to get time (travelled) from user
          double second = 0.0; //double second
          System.out.print("Enter the second number: "); //gets the second number from user
          s = buffer.readLine(); //reads a line from the console
          second = Double.parseDouble(s);//gets the double value from string variable 
          System.out.println("The quotient of " + f + " and " + s +  " is " + (first / second)); //output
        }
      }
    }

  4. #4
    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: The operator / is undefined for the argument type(s)

    Oh! 'S'cuse me! I assumed that 'f' and 's' were numerical variables. Why aren't they? I think you mean to use 'first' and 'second' rather than 'f' and 's'.

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

    boyjam2 (September 28th, 2013)

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: The operator / is undefined for the argument type(s)

    Yes I did, thanks!

    You can close this thread and mark it as solved.
    Thanks for the help, much appreciated.

  7. #6
    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: The operator / is undefined for the argument type(s)

    I can't, but I think you can. You're welcome.

  8. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: The operator / is undefined for the argument type(s)

    Quote Originally Posted by GregBrannon View Post
    I can't, but I think you can. You're welcome.
    Just looked at the next question and it says I need to display the answer I get from the question above (the one you helped me answer) as a % so basically for the code above if I did first number 25 and second number 5 I need to show the answer (5) as a % which I have no idea how to do.

  9. #8
    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: The operator / is undefined for the argument type(s)

    A quotient is multiplied by 100 to give the 'percent'. For 25/5, the result '5' is "500 percent." For another example with a quotient less than 1, let's say 5 / 25 = 0.2, multiplying by 100 gives the "percent" answer as "20 percent."

    Hope that helps explain it.

Similar Threads

  1. Replies: 5
    Last Post: September 19th, 2013, 07:30 AM
  2. Replies: 8
    Last Post: May 6th, 2013, 07:53 AM
  3. The operator + is undefined for the argument type(s) String, void
    By Proletariat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 7th, 2012, 12:10 AM
  4. Replies: 3
    Last Post: October 28th, 2011, 04:42 PM
  5. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM

Tags for this Thread