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

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation he operator / is undefined for the argument type(s) String, int

    	import java.io.*;
     
    public class FormatTime
    {
     
    public static void main(String[] args) 
    {
     
    //this will fetch our input values
    BufferedReader usrInput = new BufferedReader( new InputStreamReader( System.in) );
     
    System.out.println("Enter a number in seconds to format: ");
    String secs = usrInput.readLine();
     
    int hours = (secs / 3600);
    int remainder = (secs % 3600);
    int minutes = (remainder / 60);
    int seconds = (remainder % 60); 
     
    String Hour = (hours < 10 ? "0" : "") + hours;
    String Min = (minutes < 10 ? "0" : "") + minutes;
    String Sec = (seconds < 10 ? "0" : "") + seconds ;
     
    System.out.println(Hour +":"+ Min+":"+Sec);
    }
     
    }

    Could someone help me out with this error. I have googled but I just dont get it. I am new, very new. Started a online class 3 weeks ago and I have my first program due. My book just showed up today and I am way behind. this is the error I am getting.

    "The operator / is undefined for the argument type(s) String, int"


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: he operator / is undefined for the argument type(s) String, int

    The problem is here:

    String secs = usrInput.readLine();

    int hours = (secs / 3600);
    int remainder = (secs % 3600);

    You are trying to divide secs by 3600, but secs is a String and 3600 is an int.
    So you are trying to do: String / int, that's why you're receiving that error.

    To fix this, make secs and Integer like this:
    String secString = usrInput.readLine();
    int secs = Integer.parseInt(secString);

    or achieve the same result by this:

    int secs = Integer.parseInt(userInput.readLine());

    Both statements will do the same.

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

    mtbr00x (September 8th, 2009)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: he operator / is undefined for the argument type(s) String, int

    or this:
    int secs = new Scanner(System.in).nextInt();
    int hours = secs/3600;
    //...

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

    mtbr00x (September 8th, 2009)

Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  2. Java error "java.lang.StackOverflowError"
    By sanatkumar in forum Exceptions
    Replies: 2
    Last Post: July 7th, 2009, 02:40 PM
  3. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM
  4. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM