Results 1 to 20 of 30
Thread: Need some basic help
- 01-20-2011, 02:52 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Need some basic help
I have to make the program to where it outputs "You were born on (DD-MM-YYYY, these are the user inputs).
I have everything besides that..can anyone help?
import java.util.Scanner;
//This program does math
public class LastTest
{
public static void main(String []args)
{
Scanner in=new Scanner(System.in);
System.out.println("One last test");
System.out.print("Enter your birthday (mm/dd/yyyy): ");
String roar=in.nextLine();
int n1=Integer.parseInt(roar);
int n1=roar.indexOf("/");
int n2=roar.indexOf("/");
System.out.println
}
}
- 01-20-2011, 03:01 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
just get the values from the user, and then print a line that says:
Java Code:System.out.println("You were born on " + month + "/" + date + "/" + year);
- 01-20-2011, 03:03 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
But how do i declare what the month,date, and year is? Isn't it charAt(0,3)?
- 01-20-2011, 03:10 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
There are multiple ways you can do it, you can create a 3 item array and loop asking for a number filling the array as you go, then use the items in the array as m, d, y.
A better approach however, would probably be to ask for the date in mm/dd/yyyy format, then use a string tokenizer to store them in three new Strings names m, d, y. then convert those strings to ints named month, date, and year.
There are other ways to do this as well, a few that comes to mind is a loop from 0-2, which depending on what i is at prompts the user for the correct item and stores it in a variable, you could accomplish this with a switch, or if/else clauses.Last edited by sunde887; 01-20-2011 at 03:13 AM.
- 01-20-2011, 03:11 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
I am in basic programming and understand nothing of that..im sorry.
- 01-20-2011, 03:12 AM #6
You can use 2 SimpleDateFormat objects. One to parse input. One to format output.
- 01-20-2011, 03:13 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
I learn way better if someone shows me the code/writes it for me..that way i can see how they did it. I know nothing about programming, so what everyone here is saying is just jumbled words to me..ha
- 01-20-2011, 03:20 AM #8
I'm sure you do. Saves having to do your own work huh?
If someone mentions something you don't understand tehn ask for clarification. You have been given 2 suggestions: StringTokenizer and SimpleDateFormat. Have you bothered to read about those classes and see what they do? Another alternative is String.split() method. Go to the API and read about that too.Last edited by Junky; 01-20-2011 at 03:22 AM.
- 01-20-2011, 03:22 AM #9
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
- 01-20-2011, 03:22 AM #10
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
put your codes in code tags next time.
you simply split on the string the user entered with delimiter "/". Then print your results.Java Code:import java.util.Scanner; //This program does math public class LastTest { public static void main(String []args) { Scanner in=new Scanner(System.in); System.out.println("One last test"); System.out.print("Enter your birthday (mm/dd/yyyy): "); String roar=in.nextLine(); String[] s = roar.split("/"); for( String str : s){ System.out.println(str); } } }
Note, assuming user entered correct data each time. you will have to do your own error checking.
- 01-20-2011, 03:24 AM #11
Where would we be without a bit of spoonfeeding?
- 01-20-2011, 03:26 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
- 01-20-2011, 03:30 AM #13
Well, you are going to be a valuable asset to not only the IT industry but the human race.
- 01-20-2011, 03:30 AM #14
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Good thing my major is not computers, and i am just taking it for my own knowledge.
Thank you, and good bye.
- 01-20-2011, 03:32 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I don't want to give you the answer directly, because then there is a good chance you will continue not knowing what I am saying.
I will however help you out.
First things first, since Im assuming loops, and switches might be a bit unfamiliar to you, I will explain using a string tokenizer.
The first thing you need to do is declare a variable called date, this is the easiest possible way to accomplish this and involves no manipulation of any string objects.
When you ask for a date, use the Scanner.nextLine() method to store it in date variable, when you print it out you can use the date variable.
Some more involved methods involve creating multiple string variables:
Then you would use scanner.nextline() to store the input in some variable, and use a string tokenizer to split it up.Java Code:String date; String month, day, year;
declare this after you have stored the input in dateJava Code://create a string tokenizer object StringTokenizer st = new StringTokenizer{date, "/");
then, with each call to st.nextToken(); it will allow you to store information in some variable, each application captures everything up to the next token, which in this case is "/".
with this method you can change the format of the date if necessary, whereas the other method prints exactly what they typed.Java Code:month = st.nextToker(); ... System.out.println(... + month + "/" + day + "/" + year);
Last edited by sunde887; 01-20-2011 at 03:34 AM.
- 01-20-2011, 03:32 AM #16
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
I cannot figure out what you did there. I have to have the final line say "You were born on (DD-MM-YYYY)", but when i try to type anything in, it gives me multiple lines of coding? Like right now it says "
12
You were born on 18
You were born on 1991"
And i do know how to do most of the stuff i am doing right now, i just got done writing another code that seems to be way more complicated than this..so its not like i don't know what im doing at all, just at certain parts.
- 01-20-2011, 03:35 AM #17
Well I would love to help but I've been told to .... off!
- 01-20-2011, 03:36 AM #18
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Because you obviously aren't here to help, but to bust my balls for asking for some simple help.
- 01-20-2011, 03:43 AM #19
I have no problem with you asking for help but that isn't what you did. You asked someone to write the code for you. That denotes you as lazy and/or a cheater. I have no intention of enabling you in either case.
- 01-20-2011, 03:44 AM #20
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Im glad you are trying to learn, but even if people are busting your balls, be nice, when asking a question, try to include how you are approaching it and where you got lost. his code uses System.out.println(str), using System.out.print(str) will keep it all on one line, then you will have to take care of formatting
if it isn't the last item in the array System.out.print(str + "/");
if it is use System.out.print(str);Last edited by sunde887; 01-20-2011 at 03:50 AM.
Similar Threads
-
Basic GUI help
By vahshir in forum New To JavaReplies: 5Last Post: 10-11-2010, 01:33 AM -
Need really basic help!
By anthonyalaan in forum New To JavaReplies: 9Last Post: 09-12-2010, 01:43 PM -
Basic Help
By robjames in forum New To JavaReplies: 3Last Post: 02-09-2009, 02:58 AM -
Need some really basic help
By Mayur in forum New To JavaReplies: 6Last Post: 01-24-2009, 06:00 AM -
help with basic example
By fred in forum New To JavaReplies: 1Last Post: 07-20-2007, 05:45 PM


LinkBack URL
About LinkBacks


Bookmarks