Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Question Difference between 2 dates using Scanner

    Hey all. Just started with java today. Wanted to make a program that could calculate de difference between 2 dates in days. The dates could be arguments inserted on console with the following format DD MM YYYY DD MM YYYY. Was searching a bit and saw that is possible with Scanner but guess I'm not being succeed since when I try to run it never ends the process of compiling... I'm a bit unaware about how should I test it. Gonna paste my code and all I wanted is some feedback. Thanks in advance

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class DateDifference {
    
        
        public static void main(String[] args) {
            
                 
            Calendar c1=Calendar.getInstance();
            Calendar c2=Calendar.getInstance();
            
            
            Scanner scan = new Scanner (System.in); 
            int monthb, dayb, yearb, monthe, daye, yeare;
            dayb = scan.nextInt();
            monthb = scan.nextInt();
            yearb = scan.nextInt();
            daye = scan.nextInt();
            monthe = scan.nextInt();
            yeare = scan.nextInt();
            
            c1.set(dayb, monthb, yearb);
            c2.set(daye, monthe, yeare);
            //  open up standard input
            //BufferedReader date1 = new BufferedReader(new InputStreamReader(System.in));
            
           /* c1.set(2011,5,29);  
            Calendar c2=Calendar.getInstance();  
            c2.set(2012,5,29);*/  
      
            Date d1=c1.getTime();  
            Date d2=c2.getTime();  
      
            long diff=d2.getTime()-d1.getTime();  
            int nbofdays=(int)(diff/(1000*24*60*60));
            
            System.out.println(nbofdays);  
          
        }
        
    }

  2. #2
    Kramko is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Difference between 2 dates using Scanner

    To get you started:
    The scanner doesn't check for input on its 'nextX()' methods -- it checks on it's 'hasNextX()'.

    Your code would work if you put in 6 white space separated integers in one go, but if you want to ask the user for each int independently you'll need to write a method to do so.

    I would use something like this:
    Java Code:
    public static int getInt(Scanner scan) {
        	while(true) {
        		//checks if next token can be interpreted as an int
        		if(scan.hasNextInt()) {
        			//stores the int in val
        			int val = scan.nextInt();
        			//consumes the remainder of the line so the scanner can be reused
        			scan.nextLine();
        			//returns the val
        			return val;
        		}
        		//consumes remainder of line and asks for new input
        		scan.nextLine();
        		System.out.println("Please enter a valid integer");
        	}
        }
    and then in place of calling scan.nextInt() in your code, call getInt(scan).

    There are other improvements to make -- instantiating a Calendar is an expensive operation so you may want to consider using one instance instead of two.

    Hopefully this will get you on the right track! Post again if you have new questions!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Difference between 2 dates using Scanner

    Nope! The OP's code works just fine regarding scanner input. And it doesn't matter if the values are all on one line or on individual lines.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Difference between 2 dates using Scanner

    Your day and year in the calendar set method need to be swapped.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. difference between two dates..
    By Shahnawaz in forum New To Java
    Replies: 2
    Last Post: 09-07-2011, 07:35 AM
  2. Strings as Dates and difference
    By kryptonian03 in forum New To Java
    Replies: 7
    Last Post: 03-21-2011, 01:35 AM
  3. Difference between FileReader and Scanner
    By yahyaaa in forum New To Java
    Replies: 1
    Last Post: 06-18-2010, 12:28 PM
  4. Difference of Two Dates
    By jboy in forum New To Java
    Replies: 3
    Last Post: 09-15-2009, 06:41 PM
  5. Replies: 23
    Last Post: 02-19-2009, 05:24 AM

Tags for this Thread

Posting Permissions

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