What is wrong with the below code? It gives wrong day for any date of the year.
import java.util.Scanner;
import java.util.Calendar;
public class Solution {
public static String getDay(String d, String m, String y) {
String[] days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
Calendar c = Calendar.getInstance();
c.set(Integer.parseInt(y), Integer.parseInt(m), Integer.parseInt(d));
return days[c.get(Calendar.DAY_OF_WEEK) - 1];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String m = in.next();
String d = in.next();
String y = in.next();
System.out.println(getDay(d, m, y));
}
}
See the documentation for the Calendar class: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#set-int-int-int-
The value for month is 0-indexed, so if you provide 3 as the month value, it is interpreted as "April".
It’s easiest to have the Scanner read int values rather than strings:
int m = in.nextInt();
int d = in.nextInt();
int y = in.nextInt();
System.out.println(LocalDate.of(y, m, d).getDayOfWeek());
When I feed 5 4 2018 (today’s date), I get FRIDAY, which is correct.
If you must use strings:
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("M/d/u");
String m = in.next();
String d = in.next();
String y = in.next();
System.out.println(LocalDate.parse(m + '/' + d + '/' + y, dateFormatter).getDayOfWeek());
The Calendar class you were using is long outdated, poorly designed and sometimes cumbersome to work with. Instead I recommend java.time, the modern Java date and time API. It is so much nicer. For one little thing it numbers the months of the year in the same way humans do.
Also the names of the days of the week are built-in, so don’t reinvent the wheel. Your strings coincide with the names of the values of the DayOfWeek enum in java.time, so just print those to get the strings you want. If you don’t want all uppercase or you want the day names in another language, use DayOfWeek.getDisplayName or a DateTimeFormatter.
Link: Oracle tutorial: Date Time explaining how to use java.time.
Related
I have a date of birth in string in Date class and I want to call it as Date variable in another class using constructor, HealthProfile as per my uml diagram. How can I do that? And also, what is the Scanner input type for Date? I have done other parts and below code is my getDateString method in Date class.
public String getDateString()
{
String dob = String.valueOf(day) + month + String.valueOf(year);
return dob;
}
The date of birth format I was trying to print is, example: 8 October 2000. I have a txt file of such data and trying to call from it.
java.util.Scanner hasn’t got a method for reading an instance of your Date class (it may be obvious). I suggest that you use
nextInt() for reading the day of month (e.g., 8)
next() for reading the month name (e.g., October)
nextInt() for reading the year (e.g., 2000)
With the three values thus obtained you can call your constructor Date(int day, String month, int year). As you may know, a Scanner can read from a text file.
Aside from this (ignore if you can’t use it):
For the conversion back to a string I suggest that you put spaces between the elements to get like 8 October 2000 again rather than 8October2000, which is unpleasant to read.
One may consider an enum for the months. It will complicate input, but will provide validation that a correct month name is entered. If this is not part of your requirement, you may leave this comment for other readers.
For production work one would use LocalDate from java.time, the modern Java date and time API, and not develop one’s own Date class.
Link: Oracle tutorial: Date Time explaining how to use java.time.
I help you to create a Date Class with a contruct method.
In main class, i help you to use System.in to input data string and return what you method want.
Date Class:
public class Date {
int year;
String month;
int day;
public Date(int year, String month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
/**
* Getter and Setter methods
*/
public String getDateString(){
String dob = String.valueOf(day) + month + String.valueOf(year);
return dob;
}
Test main class:
public static void main(String[] args) throws ParseException {
Scanner scanner = new Scanner(System.in);
String inputstr = scanner.nextLine(); //you enter 24-07-2020
String[] inputarr = inputstr.split(" ");
Date date = new Date(Integer.valueOf(inputarr[2]), inputarr[1], Integer.valueOf(inputarr[0]));
System.out.println(date.getDateString());
}
I want to create a student record using a method that is able to
take input from user about student details . My class Student should
consists of following fields : short semester,
full name,
registration number etc
.registration number of a student = concatenation of year and student number.
Eg year at which student joined = 2023,
student no. = 80,
So registration number = 2380
Plus I have been tasked to input date using class GregorianCalendar
INSIDE STUDENT CLASS:
import java.util.*;
class Student2{
String fullname;
GregorianCalendar date;
short semester;
Student2()
{
}
Student2(String name,short sem, GregorianCalendar Date)
{
fullname = name;
semester=sem;
date = Date;
}
int years = date.get(Calendar.YEAR);
String year = Integer.toString(years);
String Studno = Integer.toString(80);
String y1= year.substring(0,3);
String Reg = y1.concat(Studno);
int reg = Integer.parseInt(Reg);
void Studarry()
{
int n=5,i;
Student2[] stuarry = new Student2[10];
for(i=0;i<n;i++)
{
System.out.println("Enter name sem year month day gpa cgpa\n");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
short sem2 = sc.nextShort();
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int day2=sc.nextInt()
GregorianCalendar gc2 = new GregorianCalendar(year2,month2,day2);
stuarry[i] = new Student2(name,sem2,gc2);
}
}
void Display()
{
}
}
INSIDE DRIVER CLASS:
public class Greg2{
public static void main(String[] args)
{
Student2 starr = new Student2();
starr.Studarry();
}
}
ERRORS :
Exception in thread "main" java.lang.NullPointerException
at oop2/lab5.Student2.<init>(Greg2.java:23)
at oop2/lab5.Greg2.main(Greg2.java:68)
Class name versus variable name
date = Date;
Date with an uppercase D is the name of a class, not a variable. Instead you should have defined the name of the argument being passed as date not Date. This line becomes date = date ;. The compiler can distinguish between the argument and the member variable. If you want more clarity for the reader, you can say this.date = date ;.
But that is a poor name for a variable. Because there is indeed two classes bundled with Java named Date, both related to GregorianCalendar, I suggest avoiding the use of date as a variable name for GregorianCalendar object – just too confusing.
java.time
The GregorianCalendar is a terrible class. It was supplanted entirely years ago by the java.time classes. Specifically, ZonedDateTime. Both classes represent a moment as seen through the wall-clock time of some particular region (a time zone).
However, both classes are not meant for your purpose. You want only a date, without a time-of-day and without the context of a time zone or offset-from-UTC. So LocalDate fits your needs.
LocalDate ld = LocalDate.of( year , month , day ) ;
Constructor
int years = date.get(Calendar.YEAR);
String year = Integer.toString(years);
String Studno = Integer.toString(80);
…
These lines are floating around, not placed inside a method. They should have been put inside the constructor of your method.
Why is there a class named Greg2? Did you mean a specific student? If so, Greg should be represented by values assigned to an instance of a Student class.
What is with all the 2 characters at the end of names? Naming is important; get that straight and you will be half-way to a solution.
So most of this code is a mess. Try again from scratch. Look up other code examples, such as on Stack Overflow, in the Oracle Tutorial, or in the textbook for your class of this homework assignment.
Learn about separation of concerns. One class should be just about representing a student. Another class should represent your app, and hold the main method. Use a Collection to gather the newly instantiated Student classes into a roster, possibly making a class Roster if you have other roster-related responsibilities.
Lastly, take baby steps. Add one little thing at a time, see that it runs properly. Use System.out.println to verify values. Do not try to write all the code at once.
Your NullPointerException comes from this line:
int years = date.get(Calendar.YEAR);
Field initializers like this one are executed before the constructor. So when you create a Student2 object using new Student2(), the above line is created while the date field is still null, and therefore the call to date.get() throw the exception.
Instead move the initialization of years into the constructor, after you have assigned an object to date.
As others have said too, the GregorianCalendar class is poorly designed and long outdated. If it wasn’t for a lazy teacher apperently with no clue of what has been going on with Java the last more than 5 years, you shouldn’t use it. Never ever.
Mistakes corrected as-
1) Bought field initializers inside my constructor to get rid of NullpointerException as said by ( Basil Bourque , Ole V.V. )
2) created array of student objects in main and called method Stduarry on them .
3) Variable name Date changed to gc
import java.util.*;
INSIDE STUDENT CLASS:
class Student22{
String fullname;
GregorianCalendar date;
short semester;
int reg;
Student22()
{
}
Student22(String name,short sem, GregorianCalendar gc)
{
fullname = name;
semester=sem;
date = gc;
int years = date.get(Calendar.YEAR);
String year = Integer.toString(years);
System.out.println(year);
String Studno = Integer.toString(80);
String y1= year.substring(2,4);
System.out.println(y1);
String Reg = y1.concat(Studno);
System.out.println(Reg);
reg = Integer.parseInt(Reg);
System.out.println(reg);
}
void Studarry(int n)
{
System.out.println("Enter name sem year month day \n");
Scanner sc = new Scanner(System.in);
fullname = sc.nextLine();
System.out.println(fullname);
semester = sc.nextShort();
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int day2=sc.nextInt();
GregorianCalendar gc2 = new GregorianCalendar(year2,month2,day2);
date= gc2;
int years = date.get(Calendar.YEAR);
String year = Integer.toString(years);
String Studno = Integer.toString(n);
String y1= year.substring(0,3);
String Reg = y1.concat(Studno);
reg = Integer.parseInt(Reg);
Display();
}
void Display()
{
System.out.println(fullname);
System.out.println(semester);
System.out.println(reg);
System.out.println(date.get(Calendar.YEAR));
}
}
INSIDE DRIVER CLASS:
public class Greg2{
public static void main(String[] args)
{
System.out.println("Please enter a Firstname , MiddleName & Lastname separated by spaces");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
GregorianCalendar gc = new GregorianCalendar(2018,7,22);
Student22 s = new Student22(name,(short)3,gc);
s.Display();
int i,j,n;
System.out.println("Enter n\n");
n = sc.nextInt();
Student22[] starr = new Student22[n+1];
for(j=1;j<=n;j++)
{
starr[j]= new Student22();
starr[j].Studarry(j);
}
}
}
I am using BlueJ. My assignment is to complete the method "zool" and then complete the methods "printAmerican" and "printEuropean". Then, complete the main method to call the other three; but, I get these errors:
Recompile with -Xdiags:verbose to get full ouput.
Incompatible type-cannot convert String to int for this line:
printAmerican ("enter day of the week","enter month", "enter date", "enter year");
Below is all the code:
public class Lab6 {
public static void main(String[] args) {
zool (11, "name of your pet", "name of your street");
// call the method print American
// call the method printEuropean
printAmerican ("enter day of the week","enter month", "enter date", "enter year");
printEuropean ("enter day of the week","enter month", "enter date", "enter year");
}
public static void zool (int a, String pet, String street){
//print the values of a, pet, and street
a = 11;
pet = "Fluffy";
street = "Broadway";
System.out.println(a);
System.out.println(pet);
System.out.println(street);
}
public static void printAmerican(String day, String month, int date, int year){
//print in American format. Example: Sunday, September 13, 2015.
day = Sunday;
month = September;
date = 13;
year = 2015;
System.out.println(printAmerican);
}
public static void printEuropean(String day, int date, String month, int year){
//print in Europea format. Example: Sunday, 13 September, 2015.
day = Sunday;
date = 13;
month = September;
year = 2015;
System.out.println(printEuropean);
}
}
There are several problems with your code, such as:
the method printAmerican() takes arguments String, String, int, int but you are calling it with String, String, String, String
similarly with printEuropean()
you are supposed to use the arguments that were passed to the methods, but instead you are assigning new values to them
System.out.println(printAmerican); doesn't make sense - printAmerican is a name of a method, you can't print it with println() like that
the methods currently aren't doing anything useful even if they could be compiled (with the possible exception of zool() which would do what the comment says if you removed the variable assignments)
Please review any materials you have for this assignment, and ask if you have a specific question about some problem.
I have about 3 weeks java experience so bear with me. I'm trying to make a program that calculates how many days I've been alive by following my teachers template but for the life of me I can't figure out why I keep getting the "error ';' expected.
public class age {
public static void main(String[] args) {
System.out.print("Enter your date of birth");
int calcDays = (String birthdate);{
String m1 = birthdate.substring(0,1);
String m2 = birthdate.substring(3,4);
String m3 = birthdate.substring(6,9);
int month = Integer.parseInt(m1);
int day = Integer.parseInt(m2);
int year = Integer.parseInt(m3);
int dd = year * 365 + month * 30 + day;
return(dd);
}
System.out.print("This is a test" + dd);
}
}
It looks like you wandered from the template a bit. Here's what I think was meant:
import java.util.Scanner;
public class age {
public static void main(String[] args) {
Scanner sysin = new Scanner(System.in);
System.out.print("Enter your date of birth");
String birthdate = sysin.next();
int dd = calcDays(birthdate);
System.out.print("This is a test" + dd);
}
static int calcDays(String birthdate) {
String m1 = birthdate.substring(0, 1);
String m2 = birthdate.substring(3, 4);
String m3 = birthdate.substring(6, 9);
int month = Integer.parseInt(m1);
int day = Integer.parseInt(m2);
int year = Integer.parseInt(m3);
int dd = year * 365 + month * 30 + day;
return (dd);
}
}
The output from a sample run:
Enter your date of birth: 12/25/2000
This is a test73032
Notes:
The import statement makes the Scanner class visible.
The class statement begins the definition of a class named "age". Normal Java conventions are to capitalize the names of classes (as in "Age"), but I kept your names. The class contains two static methods named main() and calcDays().
The main() method is the starting point. It is executed by the Java runtime when you run the Age class (either through "java Age" at the command line, or by clicking on "Go" in an interactive development environment.)
main() prints a prompt, uses a Scanner to read a string, passes that string to the calcDays() method, and prints the integer value returned by calcDays().
The calcDays() methods accepts a string argument, dices that up into substring for month, day and year, converts those to integer values and computes a day number to return as a result.
Experiment with this. You'll find that even a small change in format will cause an exception and a lot of noisy-looking output. As you develop more technique, you'll find ways to adapt to reasonable variations, and politely reject unreasonable ones. The suggestion to look at the Java Tutorials is a good one, but if this is your first stab at programming in any language, then you might want to invest in a beginner's book (Head First Java, Java for Dummies, etc.) and THEN come back to the Tutorials to see the same material developed at greater depth.
I am trying to split a string into different array indexes. This string is coming from user input (through java.util.Scanner) and is being loaded into a String variable. How can I split the input from the string into different array indexes?
Also, how can I do the math functions that are implied by DOBbeing an int?
Here is my code:
import java.util.Scanner;
public class main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter date of birth (MM/DD/YYYY):");
String DOB;
DOB = input.next();
int age = 0;
age = 2013 - DOB - 1;
int age2 = 0;
age2 = age + 1;
System.out.println("You are " + age + " or " + age2 + " years old");
}
}
String[] parts = DOB.split("/");
int months = Integer.parseInt(parts[0]);
int days = Integer.parseInt(parts[1]);
int years = Integer.parseInt(parts[2]);
Then just use years instead of DOB in your calculations.
Better yet, use new Calendar() to get today's precise date, and compare against that.
Use DateTimeFormat as shown in Parse Date String to Some Java Object to parse your string into a DateTime object, and then access the members.
DateTimeFormatter format = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime dateTime = format.parseDateTime(DOB);
This uses Joda Time library.
Alternatively you can use SimpleDateFormat in a similar manner, to parse it into a Date object.
I notice you're using keyboard input to recognize the string. If the user doesn't input what you expect it will crash your program. (If you're just starting Java, this is fine; you can just run it again)
You can make it easier to split by asking them thrice too eg:
int dob[] = new Integer[3]; // integer array made from Integer class-wrapper
System.out.println("Input day");
dob[0] = Integer.parseInt(input.next());
System.out.println("Input month");
dob[1] = Integer.parseInt(input.next());
System.out.println("Input year");
dob[2] = Integer.parseInt(input.next());
You now have three integers in an array, split and ready to manipulate.
If Integer can't parse the text input as a number you'll get a NumberFormatException.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class main {
public static void main(String args[]) {
// Man you should look onto doing your
// homework by yourself, ijs.
// But here it goes, hope i make myself clear.
Scanner input = new Scanner(System.in);
System.out.println("Enter date of birth (MM/DD/YYYY):");
String DOB;
DOB = input.next();
//
int age;
// You need to know when it is today. Its not 2013 forever.
java.util.Calendar cal = java.util.Calendar.getInstance();
// ^ The above gets a new Calendar object containing system time/date;
int cur_year = cal.get(Calendar.YEAR);
int cur_month = cal.get(Calendar.MONTH)+1; // 0-indexed field.
// Cool we need this info. ill skip the day in month stuff,
// you do that by your own, okay?
SimpleDateFormat dfmt = new SimpleDateFormat("MM/dd/yyyy");
int bir_year;
int bir_month;
try {
// If you wanna program, you must know that not all functions
// will exit as it's intended. Errors happen and YOU should deal with it.
// not the user, not the environment. YOU.
Date d = dfmt.parse(DOB); // This throws a parse exception.
Calendar c = Calendar.getInstance();
c.setTime(d);
bir_year = c.get(Calendar.YEAR);
bir_month = c.get(Calendar.MONTH)+1; // 0-indexed field;
age = cur_year - bir_year;
// Well, you cant be a programmer if you dont think on the logics.
if(cur_month < bir_month ) {
age -= 1;
// If the current month is not yet your birth month or above...
// means your birthday didnt happen yet in this year.
// so you still have the age of the last year.
}
// If code reaches this point, no exceptions were thrown.
// and so the code below wont execute.
// And we have the variable age well defined in memory.
} catch(ParseException e) {
// But if the date entered by the user is invalid...
System.out.println("The date you typed is broken bro.");
System.out.println("Type a date in the correct format MM/DD/YYYY and retry.");
return; // Got errors? tell the program to quit the function.
}
// Well now we can say to the user how old he is.
// As if he/she didnt know it ^^'
System.out.println(String.format("You are %d years old", age));
// **Not tested.
}
}