Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:06:56+00:00 2026-06-15T17:06:56+00:00

I have a java programming assignment where you have to input a date on

  • 0

I have a java programming assignment where you have to input a date on a single line and it gives you a numerology (horoscope-like) report based on the date. It is assumed that the user will enter a formatted date, separated with spaces.

I can retrieve the month, day, and year of the input by using in.nextInt(). However, I also have to check that the user used a correct separating character for each part of the date, which means I just have to check whether the user used forward slashes.

When looking at my code below, I currently use charAt() to find the separating characters. The problem is that the date won’t always be 14 characters long. So a date in the form of 10 / 17 / 2004 is 14 characters long, but a date of 4 / 7 / 1992 is only 12 characters long, meaning that “slash1” won’t always be in.charAt(3), in the latter situation it would be in.charAt(2).

Does java have a method that allows something like in.nextChar()? I know that it doesn’t, but how could I just find a next character in the date?

EDIT: I forgot to reflect this originally, but my professor said that we are NOT allowed to use the String.split() method, for some reason. The thing is, I get the month, day, and year perfectly fine. I just need to check that the person used a forward slash to separate the date. If a dash is entered, the date is invalid.

public void getDate()
{
    char slash1, slash2;

    do
    {
        System.out.print("Please enter your birth date (mm / dd / yyyy): ");
        Scanner in = new Scanner(System.in);
        String date = in.nextLine();

        month = in.nextInt();
        day = in.nextInt();
        year = in.nextInt();

        slash1 = date.charAt(3);
        slash2 = date.charAt(8);
    } while (validDate(slash1, slash2) == false);

    calcNum();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:06:58+00:00Added an answer on June 15, 2026 at 5:06 pm

    I would use Scanner just to get a line. Then split() the line on whitespace and check the fields:

    import java.util.Scanner;
    import java.util.regex.Pattern;
    
    public class GetDate {
    
       int month, day, year;
    
       public static void main(String[] args)
       {
          GetDate theApp = new GetDate();
          theApp.getDate();
    
       }
    
       public void getDate()
       {
          String date;
          do
          {
             System.out.print("Please enter your birth date (mm / dd / yyyy): ");
             Scanner in = new Scanner(System.in);
             date = in.nextLine();
          } while (validDate(date) == false);
    
    
          calcNum();
       }
    
       boolean validDate(String date)
       {
          // split the string based on white space
          String [] fields = date.split("\\s");
    
          // must have five fields
          if ( fields.length != 5 )
          {
             return false;
          }
    
          // must have '/' separators
          if ( ! ( fields[1].equals("/") && fields[3].equals("/") ) )
             return false;
    
          // must have integer strings
          if ( ! ( Pattern.matches("^\\d*$", fields[0]) && 
                   Pattern.matches("^\\d*$", fields[2]) &&
                   Pattern.matches("^\\d*$", fields[4]) ) )
             return false;
    
          // data was good, convert strings to integer 
          // should also check for integer within range at this point
          month = Integer.parseInt(fields[0]);
          day = Integer.parseInt(fields[2]);
          year = Integer.parseInt(fields[4]);
    
          return true;
       }
    
       void calcNum() {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my second programming assignment in my Java class, we have to create a
I have a Java programming lab-exercise in a course with students that I'd like
I have an programming assignment in Java. I have implemented it by making an
I have a java-based server that allows client applications to connect via other programming
I am a Java programming now also writing in C#. I have seen Accessor
sry about my english :) Im new to Java programming and i have a
I am new to Android and Java programming. I have a class which implements
I have been working on Eclipse recently. I am fairly new to java programming,
I'm new to Android & Java programming but have been programming in .NET for
I am programming in Java I have the code as: byte[] b = test.getBytes();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.