Welcome to Dream.In.Code
Become a Java Expert!

Join 149,597 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,894 people online right now. Registration is fast and FREE... Join Now!




Example Java string as input, representing a date, MM/DD/YYYY

 
Reply to this topicStart new topic

Example Java string as input, representing a date, MM/DD/YYYY

reddevil
3 Oct, 2007 - 01:30 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 2


My Contributions
Please give an example how to do it that including how to write leap year logic.

Here is an example of an execution:
Enter the date: 1/1/00 // input that is put from user (I use scanner method)
Your date is: January 1, 2000 this output i want to come out


Thanks
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Example Java String As Input, Representing A Date, MM/DD/YYYY
3 Oct, 2007 - 01:40 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
Please do not post in all capitals, it will not increase your replies in a good way.

We do not provide answers, we are a community to help programmers. Please make an honest attempt and we will help you with errors and incorrect output.

Please read the rules for further clarification.
User is offlineProfile CardPM
+Quote Post

reddevil
RE: Example Java String As Input, Representing A Date, MM/DD/YYYY
5 Oct, 2007 - 08:28 PM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 2


My Contributions
QUOTE(William_Wilson @ 3 Oct, 2007 - 02:40 PM) *

Please do not post in all capitals, it will not increase your replies in a good way.

We do not provide answers, we are a community to help programmers. Please make an honest attempt and we will help you with errors and incorrect output.

Please read the rules for further clarification.




well, First of all, Thanks for your suggestion.

I know using SimpleDateFormat is problably the best way to do it, but my teacher hasn't taught it yet and i assume that she doesn't want me to use it as well. Moreover, I heard she said in the class that just using Scanner and If/else logic. But that is a problem. java i have studied in my last Lab looks like this.

// ****************************************************************
// KEY Charge.java
//
// Determine interest and balance for charge card
//
// ****************************************************************
import java.util.Scanner;
import java.text.NumberFormat;

public class Charge
{
public static void main(String[] args)
{
// Declare variables here

double PreBalance,AddCharge,Inr, NewBalance,MinPayment;


//Get inputs from user

Scanner scan = new Scanner(System.in);


System.out.println("please input your Previous Balance: ");

PreBalance = scan.nextDouble();

System.out.println("please input your Additional Charge: ");

AddCharge = scan.nextDouble();

//Calculate interest owed

if (PreBalance == 0)
{
Inr = 0;
}

else
{
Inr = (PreBalance + AddCharge) * 0.02;
}


//Calculate new balance

NewBalance = PreBalance + AddCharge + Inr;



//Calculate minimum payment

if (NewBalance < 50)
{
MinPayment = NewBalance;
}

else
{
if (50 < NewBalance && NewBalance <= 300)
{
MinPayment = 50;

}

else
{
MinPayment = NewBalance * 0.2;
}
}

// print output

NumberFormat priceFormat = NumberFormat.getCurrencyInstance();

System.out.println(" CS CARD International Statement");
System.out.println( "===============================");
System.out.println( "Previous Balance: " +
priceFormat.format(PreBalance));
System.out.println( "Additional Charge: " +
priceFormat.format(AddCharge));
System.out.println( "Interest: "
+ priceFormat.format(Inr));
System.out.println( " ");
System.out.println( "New Balance: "
+ priceFormat.format(NewBalance));
System.out.println( " ");
System.out.println( "Minimum Payment: "
+ priceFormat.format(MinPayment));



}

}


If it is about integer, i understand how to do it very well. But in this assignment, there are many problems i cannot handle it such as

1. After taking a String as input from keybord, representing a date, parse the input into the form: mm/dd/yyyy

Problem is how can i take a String as input to prompt user (by scanner) and then parse it into the form: mm/dd/yyyy

2. If the user enter the month that is between 1 and 12 such as 1 or 01 how can i convert it to its complete name (January) .

I have tried many way and searched from many website, but they always reccommended me to use SimpleDateformat. how can i use it when my teacher said don't

Here is my source code i have been working on it so far.

/*
program that takes a String as input from the keyboard, representing a date
and parse the input into the form: mm/dd/yyyy.

*/


import java.util.*;



public class Assignment3

{

public static void main(String[] args)

{


// Declaring variables

String monthInput, dayInput, yearInput;
String January, February, March, April, May, June, July, August, September, October, November, December;

// Get input from user

Scanner scan = new Scanner(System.in);

System.out.print("Please enter a month : ");
monthInput = scan.next();

System.out.print("Please enter a day : ");
dayInput = scan.next();

System.out.print("Please enter a year : ");
yearInput = scan.next();

if monthInput >= 1 && monthInput <= 12)
{
January = "1"; // || January = "01";
February = "2";
March = "3";
April = "4";
May = "5";
June = "6";
July = "7";
August = "8";
September = "9";
October = "10";
November = "11";
December = "12";



}
else
{
System.out.println (" The month you provided is not valid ");
System.out.println (" Please correct it" "," "and then try again ");

}


I knew i did many mistake but please how to do it correctly. I got a long way to go to finished this such as

1. Validate that the day put is valid for the month

2. if the year entered has two charaters, convert it to an int, add 2000 to it. if has four just convert to an int


Finally, after everythng finished, it should be like this

Enter the date: 1/1/00 // user put it

your date is: January 1, 2000 // the outcome i want to come out.


So please guide me how to do it. At least, about problem the month (how to convert it to its complete name)

Thank so much

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month