Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

not able to get the  validation correct

Posted on 2014-10-11
3
Medium Priority
?
66 Views
Last Modified: 2014-10-24
Hi
i need some help on resolving this program

My input should  be HH:MM:SS

HH should be between 00 and 24
MM should be between 00 to 59
SS should be between 00 to 59
24:00:00 is a valid clock but not beyond 24:00:00 when HH is 24, MM and SS should be 00
Invalid inputs are listed below

-------------------------

nulll and empty input string
wrong delimiter such as 00|00|23,
should not accept AA:BB:CC
negative value -01:29:-04 should not accept


Pls find the program it is not validating that

import java.util.ArrayList;
import java.util.List;


public class Berlinclass {

	
	 
	 public String[] convertToBerlinTime(String time) {
	    
	        List<Integer> parts = new ArrayList<Integer>();
	        for (String part : time.split(":")) {
	            parts.add(Integer.parseInt(part));
	        }
	        return new String[] {
	                getSeconds(parts.get(2)),
	                getTopHours(parts.get(0)),
	                getBottomHours(parts.get(0)),
	                getTopMinutes(parts.get(1)),
	                getBottomMinutes(parts.get(1))
	        };
	    }

	    protected String getSeconds(int number) {
	        if (number % 2 == 0) return "Y";
	        else return "O";
	    }

	    protected String getTopHours(int number) {
	        return getOnOff(4, getTopNumberOfOnSigns(number));
	    }

	    protected String getBottomHours(int number) {
	        return getOnOff(4, number % 5);
	    }

	    protected String getTopMinutes(int number) {
	        return getOnOff(11, getTopNumberOfOnSigns(number), "Y").replaceAll("YYY", "YYR");
	    }

	    protected String getBottomMinutes(int number) {
	        return getOnOff(4, number % 5, "Y");
	    }

	    // Default value for onSign would be useful
	    private String getOnOff(int lamps, int onSigns) {
	        return getOnOff(lamps, onSigns, "R");
	    }
	    private String getOnOff(int lamps, int onSigns, String onSign) {
	        String out = "";
	        // String multiplication would be useful
	        for (int i = 0; i < onSigns; i++) {
	            out += onSign;
	        }
	        for (int i = 0; i < (lamps - onSigns); i++) {
	            out += "O";
	        }
	        return out;
	    }

	    private int getTopNumberOfOnSigns(int number) {
	        return (number - (number % 5)) / 5;
	    }

	}
		
		
	

Open in new window

0
Comment
Question by:roy_sanu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Expert Comment

by:dpearson
ID: 40375311
Whoa, that's a lot of code.

You should check out "SimpleDateFormat" - it's designed to do exactly what you're trying to do by hand.  It'll throw a ParseException if the inputs are invalid.

		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
			Calendar cal = Calendar.getInstance() ;
			String time = "14:10:20";
			cal.setTime(format.parse(time)) ;

			int hours = cal.get(Calendar.HOUR_OF_DAY) ;
			int min = cal.get(Calendar.MINUTE) ;
			int sec = cal.get(Calendar.SECOND) ;
			System.out.println(hours + ":" + min + ":" + sec) ;

Open in new window

0
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 2000 total points
ID: 40375333
24:00:00 is a valid clock but not beyond 24:00:00 when HH is 24, MM and SS should be 00

Open in new window


Does it mean 00:00:00 and 24:00:00 are same ? I believe range should be from 00:00:00 to 23:59:59.

And as mentioned by dpearson, you will need to use SimpleDateFormat. It will throw an exception if the input is not as per the specified format or invalid. You dont need to reinvent the wheel again.

Refer http://www.mkyong.com/java/how-to-check-if-date-is-valid-in-java/ to get an idea how to achieve the objective.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40375590
you will need to use SimpleDateFormat. It will throw an exception if the input is not as per the specified format or invalid. You dont need to reinvent the wheel again.
I agree, but you need to know that it IS possible to get invalid data past it unless you call setLenient(false) on the DateFormat
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question