Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Missing a line or two of code,b ut dont know what it is.

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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).



    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?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,706
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default 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:
    for( String str : s);

    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:
    String[] s = ox.split("/");
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Missing a line or two of code,b ut dont know what it is.

    Quote Originally Posted by backdown View Post
    I have to make the program to where it outputs "You were born on (DD-MM-YYYY, these are the user inputs).



    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.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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]);;
     
    		}
    	}

  5. #5
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default 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.
    Last edited by JavaHater; January 20th, 2011 at 08:29 AM.

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Missing a line or two of code,b ut dont know what it is.

    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 :

    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 :

     
    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

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default 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:

    int n1= in;

    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:

    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM
  2. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2010, 10:07 AM
  3. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 08:49 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 08:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM