Java Error: cannot find symbol yet the variables are declared?

喜夏-厌秋 提交于 2019-12-08 07:24:40

问题


import java.util.Scanner;
public class Assignment1Q3
{
    public static void main(String[] args) 
    { 
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the first time: ");
    int fTime = in.nextInt();
    System.out.print("Please enter second time: ");
    int lTime = in.nextInt();
    int tDifference = Math.abs(fTime - lTime);
    String strTDiff = String.valueOf(tDifference);
    int length = strTDiff.length();
    if (length == 4)
    {
        String hours = strTDiff.substring(0, 2);
        String minutes = strTDiff.substring(3, 5);
    }
    else if (length == 3)
    {
        String hours = strTDiff.substring(0);
        String minutes = strTDiff.substring(2, 4);
    }
    else
    {
        String hours = ("0");
        String minutes = strTDiff.substring(0, 1);
    }
    System.out.println(hours + " hours " + minutes + " minutes");
    }
}

Hey guys so im attempting to make a simple program using Java to find the difference between 2 times given in military time and to give the output in number of hours and minutes. Whenever i compile this in command prompt it gives me an error for the variable hours and the variable minutes in the last printing line saying "cannot find symbol". I thought trying to declare them before the if statement but that did not work either. I apologize but i am very new to programming and appreciate any help offered.


回答1:


By calling String within the if statements they only exist within the if statement you need to decalre them outside of the if and associate a value within the if.

import java.util.Scanner;
public class Assignment1Q3
{
    public static void main(String[] args) 
    { 
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the first time: ");
    int fTime = in.nextInt();
    System.out.print("Please enter second time: ");
    int lTime = in.nextInt();
    int tDifference = Math.abs(fTime - lTime);
    String strTDiff = String.valueOf(tDifference);
    int length = strTDiff.length();
    String hours = "";
    String minutes = "";

    if (length == 4)
    {
        hours = strTDiff.substring(0, 2);
        minutes = strTDiff.substring(3, 5);
    }
    else if (length == 3)
    {
        hours = strTDiff.substring(0);
        minutes = strTDiff.substring(2, 4);
    }
    else
    {
        hours = ("0");
        minutes = strTDiff.substring(0, 1);
    }
    System.out.println(hours + " hours " + minutes + " minutes");
    }
}

so you can initialise them to "" or to remove unnecessary else statement initialise them to the else valuse

    String hours = "0";
    String minutes = strTDiff.substring(0, 1);

    if (length == 4)
    {
        hours = strTDiff.substring(0, 2);
        minutes = strTDiff.substring(3, 5);
    }
    else if (length == 3)
    {
        hours = strTDiff.substring(0);
        minutes = strTDiff.substring(2, 4);
    }
System.out.println(hours + " hours " + minutes + " minutes");



回答2:


When you define a object reference or any primitive variable inside a if block (or any such block), it is accessible only within that block.

Change as below

final String hours, minutes;
if (length == 4)
{
    hours = strTDiff.substring(0, 2);
    minutes = strTDiff.substring(3, 5);
}
else if (length == 3)
{
    hours = strTDiff.substring(0);
    minutes = strTDiff.substring(2, 4);
}
else
{
    hours = ("0");
    minutes = strTDiff.substring(0, 1);
}


来源:https://stackoverflow.com/questions/14595878/java-error-cannot-find-symbol-yet-the-variables-are-declared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!