Java编译错误:找不到BufferedReader的符号

问题描述 投票:0回答:5

嘿,我刚刚开始编写关于java的第一本编程书,所以这应该是一个简单的解决方案。用我对条件语的新知识搞清楚,我得到了标题错误。

这是代码:

import java.io.*;

public class Hello {  
   public static void main (String args[]) { 

   BufferedReader br = new BufferedReader(ip);
   InputStreamReader ip = new InputStreamReader(System.in);

   String str;

   System.out.print("Enter your name: ");
   str = br.readLine();  

   System.out.println(str);
   System.out.println("Welcome to the world of Java Programming!"); 
  }
}

当我尝试编译时:

Hello.java:6:error:cannot find symbol
BufferedReader br = new BufferedReader(ip);

symbol: variable ip
location: class Hello
1 error
java
5个回答
4
投票

您在声明之前引用ip。将订单更改为:

InputStreamReader ip = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ip);

1
投票

试试这个 :-

import java.io.*;

public class Hello {  
   public static void main (String args[]) { 
   InputStreamReader ip = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(ip);
   String str;
   System.out.print("Enter your name: ");
   str = br.readLine();  
   System.out.println(str);
   System.out.println("Welcome to the world of Java Programming!"); 
  }
}

基本上,首先需要在使用变量之前声明变量。就像你需要在使用InputStreamReader ip = new InputStreamReader(System.in);之前声明一个变量BufferedReader br = new BufferedReader(ip);

此外,从Java Docs开始学习


0
投票

在您的缓冲读卡器中,您尝试在定义之前引用输入流读取器ip。只需交换这两行。

InputStreamReader ip = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ip);

0
投票

您之后定义ip并在创建之前使用它为什么会出现该错误

BufferedReader br = new BufferedReader(ip);
   InputStreamReader ip = new InputStreamReader(System.in);

-1
投票

试试这个

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
© www.soinside.com 2019 - 2024. All rights reserved.