Question:
In Java, Variable not initialized? Why? I declared it at the top of my code??? And checking user input too?
halo2jak
2010-10-10 10:43:27 UTC
I'm writing a time travel program where the user will enter his current time, and how many months, days, hours, minutes, and seconds he wants to travel back in time. I'm having trouble with 2 things... At the bottom of my code, it says that variables months and monthsback have not been initialized, and I don't know why.

Also, I want to try and figure out how to check the validity of the user input, and you can see that I tried, but I think it's wrong, because even if the user inputs something wrong, it goes onto the next line for the user to input something. I want it to keep repeating the line until the user inputs the right input. I'm new to java, so any help would be appreciated!


import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) {
String month;
int days, months, hours, minutes, seconds, monthsback, daysback, hoursback, minutesback, secondsback;
int timecurrent, timeback, timediff, resultseconds, resultminutes, resulthours;

Scanner keyboard = new Scanner (System.in);
System.out.println("Hi Marty!");
System.out.println("What month is it:");
month = keyboard.next();
if (!month.equalsIgnoreCase("January"))
if (!month.equalsIgnoreCase("February"))
if (!month.equalsIgnoreCase("March"))
if (!month.equalsIgnoreCase("April"))
if (!month.equalsIgnoreCase("May"))
if (!month.equalsIgnoreCase("June"))
if (!month.equalsIgnoreCase("July"))
if (!month.equalsIgnoreCase("August"))
if (!month.equalsIgnoreCase("September"))
if (!month.equalsIgnoreCase("October"))
if (!month.equalsIgnoreCase("November"))
if (!month.equalsIgnoreCase("December"))

if (month.equalsIgnoreCase("January"))
months = 1;
if (month.equalsIgnoreCase("February"))
months = 2678400;
if (month.equalsIgnoreCase("March"))
months = 5097600;
if (month.equalsIgnoreCase("April"))
months = 7776000;
if (month.equalsIgnoreCase("May"))
months = 10368000;
if (month.equalsIgnoreCase("June"))
months = 13046400;
if (month.equalsIgnoreCase("July"))
months = 15638400;
if (month.equalsIgnoreCase("August"))
months = 18316800;
if (month.equalsIgnoreCase("September"))
months = 20995200;
if (month.equalsIgnoreCase("October"))
months = 23587200;
if (month.equalsIgnoreCase("November"))
months = 26265600;
if (month.equalsIgnoreCase("December"))
months = 28857600;
System.out.println("That isn't a month Marty!");
System.out.println("What day in " + month + ":");
days = keyboard.nextInt();
System.out.println("Enter current hour:");
hours = keyboard.nextInt();
System.out.println("Enter current minute:");
minutes = keyboard.nextInt();
System.out.println("Enter current second:");
seconds = keyboard.nextInt();
System.out.println("How many days do you want to travel back:");
daysback = keyboard.nextInt();
System.out.println("Enter how many hours you want to travel back:");
hoursback = keyboard.nextInt();
System.out.println("Enter how many minutes you want to travel back:");
minutesback = keyboard.nextInt();
System.out.println("Enter how many seconds you want to travel back:");
secondsback = keyboard.nextInt();

It's right here where the months and monthsback have a red line underneath them, saying they are not initialized.

timecurrent = (months + (days*60*60*24) + (hours)*60*60) + ((minutes)*60) + seconds;
timeback = monthsback + (daysback*60*60*24) + (hoursback*60*60) + (minutesback*60) + secondsback;
timediff = (86400 + (timecurrent - timeback)) % 86400;
resultseconds = timediff % 60;
timediff = timediff / 60;
resultminutes = timediff % 60;
timediff = timediff / 60;
resulthours = timediff;
System.out.println("When you arrive, it will be: " ); //This is how far i've gotten so far.
Three answers:
auddrey_nw
2010-10-10 11:10:36 UTC
Initializing variable means set initial value for them. You have declared months and moentnthsback but have not set any initial value for them, more ever you have added other values with. it like add something to nothing.

you should initialize your declared variable mainly those which are not being set with value before they are being used in operation.

int months=0, ....monthback=0..;
2010-10-10 10:55:09 UTC
There's a difference between a variable being "declared" and being "initialized". You have "declared" all your variables up at top.



A variable becomes initialized when it has a value. You code is NEVER setting a value to monthsback or months. Perhaps you want to add code for them like you have for hours, minutes, seconds, etc. This is probably just an oversight.
2016-06-03 03:29:58 UTC
Top 3 #1 jams : 1. Sharp Dressed Man 2. Legs 3. Gimme All Your Lovin'


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...