Reputation: 11
Java program that first asks the user for a list of dates, and then prints the earliest date, the latest date, and the average year of the dates, I can't get it to compile to save my life, having trouble declaring methods in other methods, I am very new to programming.
import java.util.*;
public class Proj6 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a list of dates: ");
String list = s.nextLine();
StringTokenizer st = new StringTokenizer(list, ", ");
String[] dates = new String[st.countTokens()];
for (int i = 0; i < dates.length; i++) {
dates[i] = st.nextToken();
}
Proj6.printEarliest(dates);
Proj6.printLatest(dates);
Proj6.printAvgYear(dates);
}
/**
* getMonth returns the month in a date
* of the form month/day/year
*
* @param date - A date of the form month/day/year
* @return The month of the given date
*/
public static int getMonth(String date) {
StringTokenizer st = new StringTokenizer(date, "/");
return Integer.parseInt(st.nextToken());
}
/**
* getDay returns the day in a date
* of the form month/day/year
*
* @param date - A date of the form month/day/year
* @return The day of the given date
*/
public static int getDay(String date) {
StringTokenizer st = new StringTokenizer(date, "/");
st.nextToken();
return Integer.parseInt(st.nextToken());
}
/**
* getYear returns the year in a date
* of the form month/day/year
*
* @param date - A date of the form month/day/year
* @return The year of the given date
*/
public static int getYear(String date) {
StringTokenizer st = new StringTokenizer(date, "/");
st.nextToken();
st.nextToken();
return Integer.parseInt(st.nextToken());
}
/**
* printEarliest prints the date that comes chronologically
* first from the dates array
*
* @param dates - An array of dates, all of the form month/day/year
*/
public static void printEarliest(String[] dates) {
int i = 0;
Proj6.getYear(dates);
Proj6.getMonth(dates);
Proj6.getDay(dates);
for (i= 0; i < dates.length; i++) {
if(getYear.dates [i]<=getYear.dates[i])
st.nextToken();
}
System.out.println("Earliest date:" + dates[i]);
}
/**
* printLatest prints the date that comes chronologically
* last from the dates array
*
* @param dates - An array of dates, all of the form month/day/year
*/
public static void printLatest(String[] dates) {
}
/**
* printAvgYear prints the average year among all the dates in
* the dates array
*
* @param dates - An array of dates, all of the form month/day/year
*/
public static void printAvgYear(String[] dates) {
proj6.getYear();
int sum = 0;
int avg = 0;
for (int i = 0; i < dates.length; i++) {
sum = getYear + sum;
dates[i] = st.nextToken();
}
avg = sum/dates.length;
System.out.println("Average:" + avg);
} }
Upvotes: 1
Views: 306
Reputation: 28389
In Java you don't declare methods inside of other methods (like you can in javascript).
You have a class that contains all your methods. They can call each other.
When you say "asks" do you mean you need the program that's running in the terminal to ask a question and then wait for input and then act when the user inputs something?
If that's the case, if you wanted to ask the user for their username, for example, you would do this:
Console console = System.console();
String username = console.readLine("User Name? ");
This would hang out waiting for the [enter]
key and collect whatever was in the keyboard buffer into your username String... you could then proceed with acting on it.
And welcome to Stack. Please don't forget to mark answers as correct when your questions have been answered, and to mark up the answers that you feel are most helpful.
Upvotes: 1