Missing a line or two of code,b ut dont know what it is.
I have to make the program to where it outputs "You were born on (DD-MM-YYYY, these are the user inputs).
Code :
import java.util.Scanner;
//This program does math
public class Final
{
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);
String date;
String month, day, year;
String ox=in.nextLine();
String[] s = ox.split("/");
for( String str : s);
System.out.println("You were born on"+day+month+year);
}
}
That is what i have so far, but i need to declare the day, month and year..can anyone help me?
Re: Missing a line or two of code,b ut dont know what it is.
Ok, I have a question for you (for your benefit, not mine):
What does this line do:
Explain that to me, and you might have the answer to your question, or at least be going down the correct path.
Personally, I wouldn't even bother with a loop here. You know (assume) the input is correct, and you also know it is formatted mm/dd/yyyy. Which means when you do this:
you should know that s[0] will be the month, s[1] will be the day, and s[2] will be the year. So, you can set those variables directly accordingly.
See if you can code what I just explained. If you need help, post what you have after you try.
Re: Missing a line or two of code,b ut dont know what it is.
Quote:
Originally Posted by
backdown
I have to make the program to where it outputs "You were born on (DD-MM-YYYY, these are the user inputs).
Code :
import java.util.Scanner;
//This program does math
public class Final
{
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);
String date;
String month, day, year;
String ox=in.nextLine();
String[] s = ox.split("/");
for( String str : s);
System.out.println("You were born on"+day+month+year);
}
}
That is what i have so far, but i need to declare the day, month and year..can anyone help me?
I have already told you in another forum, the s array contains all the fields you want. if you want to get the month field, then use s[0]. Similarly the rest. Didn't you learn arrays yet? the for loop is just to display all the items inside s array. It does not necessary mean you need to use it.
Re: Missing a line or two of code,b ut dont know what it is.
I have used the arrays, i think, but i have gotten no where? It goes to where i can input everything, but it gives me an error after i hit enter.
Code :
import java.util.Scanner;
//This program does math
public class Final
{
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);
String date;
String month, day, year;
String ox=in.nextLine();
String[] s = ox.split("/");
for( String str : s);
System.out.println("You were born on"+s[0]+s[1]+s[2]);;
}
}
Re: Missing a line or two of code,b ut dont know what it is.
remove the for loop but leave the print statement. also, why are you using parseInt() ? remove it.
Re: Missing a line or two of code,b ut dont know what it is.
Code :
import java.util.Scanner;
//This program does math
public class Final
{
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= roar;
String date;
String month, day, year;
String ox=in.nextLine();
String[] s = ox.split("/");
System.out.println("You were born on"+s[0]+s[1]+s[2]);
String str;
}
}
I removed it, but how do i declare this line :
Code :
System.out.println("You were born on"+s[0]+s[1]+s[2]);
String str;
to use the month,day and year?
The way i am running it right now, i am getting this error and cannot figure out why :
Code :
C:\Users\TJ\Desktop\Final.java:13: incompatible types
found : java.util.Scanner
required: int
int n1= in;
^
1 error
Tool completed with exit code 1
Re: Missing a line or two of code,b ut dont know what it is.
This is the problem with simply copying and pasting spoonfed answers. You have to understand what's actually going on if you have any hope of writing code.
You've actually copied from two different sources, which leads me to believe that you're "shotgun debugging"- changing random things and hoping for the best, again without understanding what's actually going on.
What do you think this line does:
Your n1 variable is an int. Your in variable is a Scanner. What are you actually trying to do here? Consult the API for useful functions.
And what do you think these lines do, from your original source, which seems to be different from the source of the error:
Code :
String roar=in.nextLine();
int n1= roar;
Again, n1 is an int. Your roar variable is a String. Why do you think this would work?
Honestly, I'd suggest going back to the basic tutorials and taking the time to read through them instead of trying to hack together various spoonfed answers you've received from the internet.