Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
I have just started my java programming classes and i am trying to finish my first project.

I need some help figuring this out.

What i need to do is input the date in the format mm/dd/yyyy and then it will output it as dd-mm-yyyy.

This is probably a really simple problem that i am overlooking(or just to stupid to figure out...this is only my second java programming lab). Anyway is someone could help me out and possibly explain why i have to do those steps to get it to work that would be great. I did a search on google but that really did not help all that much.

I am going to show you the whole code for the project right now but really all you need is the stuff at the end.

PHP:
import java.util.Scanner;
import java.util.*;
import java.text.*;
public class Lab2 
{
	public static void main(String[] args)
	{		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("***************************************");
		System.out.println("* Welcome to your first Java program! *");
		System.out.println("***************************************");
		System.out.println("                                       ");
		System.out.println("*** Test integer arithmetic ***");
		System.out.println("                               ");
		System.out.println("Enter first integer number: ");
		int value1 = keyboard.nextInt();
		System.out.println("Enter second integer number: ");
		int value2 = keyboard.nextInt();
		int addvalue = value1 + value2;
		System.out.println(value1 + " + " + value2 + " = " + addvalue);
		int minusvalue = value1 - value2;
		System.out.println(value1 + " - " + value2 + " = " + minusvalue);
		int multiplyvalue = value1 * value2;
		System.out.println(value1 + " * " + value2 + " = " + multiplyvalue);
		int dividevalue = value1 / value2;
		System.out.println(value1 + " / " + value2 + " = " + dividevalue);
		int modulusvalue = value1 % value2;
		System.out.println(value1 + " % " + value2 + " = " + modulusvalue);
		System.out.println("                            ");
		System.out.println("*** Test real arithmetic ***");
		System.out.println("                            ");
		System.out.println("Enter first real number: ");
		double real1 = keyboard.nextDouble();
		System.out.println("Enter second real number: ");
		double real2 = keyboard.nextDouble();
		double addreal = real1 + real2;
		System.out.println(real1 + " + " + real2 + " = " + addreal);
		double minusreal = real1 - real2;
		System.out.println(real1 + " - " + real2 + " = " + minusreal);
		double multiplyreal = real1 * real2;
		System.out.println(real1 + " * " + real2 + " = " + multiplyreal);
		double dividereal = real1 / real2;
		System.out.println(real1 + " / " + real2 + " = " + dividereal);
		System.out.println("                              ");
		System.out.println("*** Test String operations ***");
		System.out.println("                              ");
		keyboard.nextLine();
		System.out.println("Enter a string of characters: ");
		String typedwords = keyboard.nextLine();
		System.out.println("The length of string"+ " \"" + typedwords + "\"" + 
				" is " + typedwords.length());
		System.out.println("Enter an integer between 0 and 20: ");
		int stringint = keyboard.nextInt();
		System.out.println("The character at index " + stringint + 
				" of string " + "\"" + typedwords + "\"" + " is " + 
				"\'" + typedwords.charAt(stringint) + "\'");
		keyboard.nextLine();
		System.out.println("Enter another string of characters: ");
		String typedword = keyboard.nextLine();
		System.out.println("The first occurence of string " + "\"" + 
				typedword + "\"" + " in string " + "\"" + typedwords + 
				"\"" + " is at position " + typedwords.indexOf(typedword));
		System.out.println("                     ");
		System.out.println("*** One last test ***");
		System.out.println("                     ");
		System.out.println("Enter your birthday (mm/dd/yyyy): ");
		
		
		
		
	}
}

Please help me out!!

Thanks
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
plinden said:
I suggest looking at SimpleDateFormat as well as DateFormat

Hint, use the parse method to get the Date object, then the format method to format the output.

Very good ideas and even better, the fact that you didn't do the research for him. It's far too easy to request the answer and have someone give it to you, but Java is all about research and you don't find anything until you've dug through the documents.

By the way, another suggestion: O'Reilly's Java in a Nutshell. It's the book that is both a reference and a tutorial of sorts, full of examples.
 

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
bousozoku said:
Very good ideas and even better, the fact that you didn't do the research for him. It's far too easy to request the answer and have someone give it to you, but Java is all about research and you don't find anything until you've dug through the documents.

By the way, another suggestion: O'Reilly's Java in a Nutshell. It's the book that is both a reference and a tutorial of sorts, full of examples.

Thanks for pointing out the book. I really do learn better from examples then just stuff being shoved at me and saying figure it out. While it is satisfying when you do figure it out i learn better from examples.

Thanks for a point in the right direction guys.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
If you convert it to a string, its really easy to switch the formats, using the 'replace' method in the String API.

Search google for 'String +java' for more info.

import java.util.Scanner;

public class test {
public static void main (String args[]) {
//insert your other code here.

// reading in the data
Scanner myScanner=new Scanner(System.in);
System.out.println("Please enter your birthday (dd/mm/yyyy)");
String theDate=myScanner.next();
theDate=theDate.replace("/","-");
System.out.println(theDate);​
}​
}
If uses a different method of reading in data, and doesn't check for errors in the date (eg if you entered Frog it'd return Frog)
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
Eraserhead: You forgot to switch the dd and mm around.

Solution using Scanner without error checking:
Code:
String mm = keyboard.next("\d*");
keyboard.skip("/");
String dd = keyboard.next("\d*");
keyboard.skip("/");
String yyyy = keyboard.next("\d*");
System.out.println(dd+"-"+mm+"-"+yyyy);
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
Eraserhead said:
If you convert it to a string, its really easy to switch the formats, using the 'replace' method in the String API.

Search google for 'String +java' for more info.

import java.util.Scanner;

public class test {
public static void main (String args[]) {
//insert your other code here.

// reading in the data
Scanner myScanner=new Scanner(System.in);
System.out.println("Please enter your birthday (dd/mm/yyyy)");
String theDate=myScanner.next();
theDate=theDate.replace("/","-");
System.out.println(theDate);​
}​
}
If uses a different method of reading in data, and doesn't check for errors in the date (eg if you entered Frog it'd return Frog)

Sorry, but that's a kludge and not very configurable. Example - write a program to read a properties file to get the output date format, or take it from the command line. Character replacement doesn't handle that very well.
 

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
I looked through all the info you gave me and while i was doing that i was thinking to myself that this was stuff we had not learned yet. It would get the job done and probably better then the way i came up with but my way does work.

Here is what i ended up doing:
PHP:
System.out.println("Enter your birthday (mm/dd/yyyy): ");
		String birthday = keyboard.nextLine();
		System.out.println("You were born on " + birthday.substring(3,5) + 
				"-" + birthday.substring(0,2) + "-" + birthday.substring(6,10));

We have not learned the date stuff yet so i figured i should stay with stuff we had.

Thanks for the help guys. When it comes to dates i should be ahead of the game.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.