Question

In: Computer Science

In Java, how can I convert a string user input for example a student starts a...

In Java, how can I convert a string user input for example a student starts a uni degree in "Winter/2022", to a date type, and then once I have done that, add 3 years to that value, and changing the season, so for example Student Started: Winter/2022 and Student Finished: Summer/2025. Is there any possible way to do this?

Thank you.

Solutions

Expert Solution

code.......

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;

public class StringToDate {
public static void main(String[] args)throws Exception {
Scanner sc=new Scanner(System.in);
  
System.out.println("Enter current date in format (dd/mm/yyyy) with season put space between date and season");
String input=sc.nextLine();
String date=input.substring(0,input.indexOf(' '));// Retrieving data part from the given string
String season=input.substring(input.indexOf(' ')+1);// Retrieving season part from the given string
  
//taking the position of last slash or hypen in the inputted string
int pos=input.lastIndexOf('/');// this method returns -1 if / is not present.
  
int yearPart=Integer.parseInt(input.substring(pos+1,pos+5));
  
System.out.println("Enter the year to which will be added to the current year:");
int yearAdd=sc.nextInt();
  
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(date);
System.out.println("Current date "+date1+" Season:"+season);
  
  
//loop to decide the season change
int i=1;
while(i<=yearAdd){
  
if(i%3==0)
{
if(season.equalsIgnoreCase("Summer"))
season="winter";
else
season="summer";
  
}
i++;
}
  
System.out.println("Date after "+yearAdd+" years");
yearPart=yearPart+yearAdd;
  
String updateDate=date.substring(0,pos-1)+"/"+yearPart;
date1=new SimpleDateFormat("dd/MM/yyyy").parse(updateDate);
System.out.println("Current date "+date1+" Season:"+season);
  

}
}

// Input and Output part

Enter current date in format (dd/mm/yyyy) with season put space between date and season
12/12/2020 Winter
Enter the year to which will be added to the current year:
3
Current date Sat Dec 12 00:00:00 IST 2020 Season:Winter
Date after 3 years
Current date Thu Jan 12 00:00:00 IST 2023 Season:summer


Related Solutions

JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
I need convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
JAVA PROGRAMMING Is there a way I can use a method to place a user input...
JAVA PROGRAMMING Is there a way I can use a method to place a user input variable into an array? Then call the same method to print the array? I'm new to Java programming I'm not sure how to continue. For example: import java.util.Scanner; public class PartayScanner { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter pokemon 1:"); String pokemon1 = scan.nextLine(); System.out.println("Enter pokemon 2:"); String pokemon2 = scan.nextLine(); System.out.println("Enter pokemon 3:"); String pokemon3 = scan.nextLine();...
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include #include #include #include #include #include #include #include #define INPUT_SIZE 8 using namespace std; // Function...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include<iostream> #include<string> #include<exception> #include <cstdlib> #include<stdio.h> #include<map> #include <cctype> #include<Windows.h> #define INPUT_SIZE 8 using namespace std;...
In Java, I need a program that will ask a user to input how many tests...
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc....
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT