Cannot find symbol if statement error

半腔热情 提交于 2019-12-24 18:07:55

问题


I have been coding a small program for fun, but I have been getting this error:

Compilation error   time: 0.11 memory: 380672 signal:0Main.java:22: 
error: cannot find symbol
            string dtext = "One";
        ^
  symbol:   class string
  location: class Ideone
Main.java:37: error: cannot find symbol
        System.out.println(dtext);
                       ^
  symbol:   variable dtext
  location: class Ideone
2 errors

My code:

import java.util.*;
import java.lang.*;
import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;
import java.lang.String;

class Ideone
{
public static void main (String str[]) throws IOException
{
    Scanner sc = new Scanner(System.in);

    //System.out.println("Please enter the month of birth");
    //int month = sc.nextInt();
    System.out.println("Please enter the day of birth");
    int day = sc.nextInt();

    //day num to day text
    if (day == 1)
    {
        string dtext = "One";
    }
    else if (day == 2)
    {
        string dtext = "Two";
    }
    else if (day == 3)
    {
        string dtext = "Three";
    }
    else
    {
        System.out.println("Error, day incorrect.");
    }

    System.out.println(dtext);
}
}

I did some research and found that java cannot find the string variable, but why? The variable is defined and, the print statement is correct.


回答1:


There is no string class in java. There is String class.

string dtext = "Two";

should be

   String dtext = "Two";

S must be capital.

And have a look on your String variable scope. You are restricted it to If block.Move it to top,

Then your code look like

String dtext = "";
        if (day == 1) {
            dtext = "One";
        } else if (day == 2) {
            dtext = "Two";
        } else if (day == 3) {
            dtext = "Three";
        } else {
            System.out.println("Error, day incorrect.");
        }
        System.out.println(dtext);



回答2:


You have typo

String dtext = "One";  

Look at String class

One more thing check the variable scope

if (day == 1)
{
    String dtext = "One";  //it dtext has local scope here
}//after this line dtext is not available  

declare dtext outside if as

String dtext = "";
 if (day == 1)
 {
    dtext = "One";
 }
 else if (day == 2)
 {
    dtext = "Two";
 }
 else if (day == 3)
 {
    dtext = "Three";
 }
 else
 {
    System.out.println("Error, day incorrect.");
 }

System.out.println(dtext);



回答3:


string doesn't exist in java. Your string first letter should be capital -> String

eg

change string dtext = "One"; to String dtext = "One";

From your code

if (day == 1)
{
    string dtext = "One";
}
else if (day == 2)
{
    string dtext = "Two";
}
else if (day == 3)
{
    string dtext = "Three";
}
else
{
    System.out.println("Error, day incorrect.");
}

System.out.println(dtext);      //this line will get error dtext variable in not reachable.

Your code need to look like below

String dtext ="";
if (day == 1)
{
    dtext = "One";
}
else if (day == 2)
{
    dtext = "Two";
}
else if (day == 3)
{
    dtext = "Three";
}
else
{
    System.out.println("Error, day incorrect.");
}
System.out.println(dtext);


来源:https://stackoverflow.com/questions/19406585/cannot-find-symbol-if-statement-error

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