【问题标题】:Java - Scanner/symbol errorsJava - 扫描仪/符号错误
【发布时间】:2018-04-06 03:42:05
【问题描述】:

我对此很陌生,正在学习 Java 初学者课程。任务是搜索字符串以获取信息,但出现以下错误。

首先是代码。

/***********************************************************************
* Identify if webpage is secure or unencrypted given full HTML path    *
***********************************************************************/

import java.io.*;
import java.lang.*;
import java.util.Scanner;

public class stringy {

    public static void main (String args[]) {

    Scanner userInput = new Scanner(System.in);
    System.out.print("Paste the web address into the prompt: ");   //collect web address from user
    String webPath = scan.next();

    CharSequence seq = "https://";                     //search for https:
    boolean testCase = test.contains(seq);
    System.out.println("Found https://" + testCase);

    boolean testPositive = test.contains("https://");     //if found https, notify user
    System.out.println("This website is secure.  Continue exercising caution while browsing.");

    CharSequence seq2 = "http://";                     //search for https:
    boolean testCase2 = test.contains(seq2);
    System.out.println("Found https://" + testCase2);

    boolean testNegative = test.contains("http://");     //if found http, notify user
    System.out.println("This website is unencrypted!" + '\n');
    System.out.println("Do not post personal or sensitive information in any form or field on this page." + '\n');
    }
}

错误输出:

C:\Users\name\Documents\School\Intro to Programming (JAVA)\Week 3\stringy.java:15: error: cannot find symbol  
    String webPath = scan.next();  
                     ^  
  symbol:   variable scan  
  location: class stringy  
C:\Users\name\Documents\School\Intro to Programming (JAVA)\Week 3\stringy.java:18: error: cannot find symbol  
    boolean testCase = test.contains(seq);  
                       ^  
  symbol:   variable test  
  location: class stringy  
C:\Users\name\Documents\School\Intro to Programming (JAVA)\Week 3\stringy.java:21: error: cannot find symbol  
    boolean testPositive = test.contains("https://");     //if found https, notify user  
                           ^  
  symbol:   variable test  
  location: class stringy  
C:\Users\name\Documents\School\Intro to Programming (JAVA)\Week 3\stringy.java:25: error: cannot find symbol  
    boolean testCase2 = test.contains(seq2);  
                        ^  
  symbol:   variable test  
  location: class stringy  
C:\Users\name\Documents\School\Intro to Programming (JAVA)\Week 3\stringy.java:28: error: cannot find symbol  
    boolean testNegative = test.contains("http://");     //if found http, notify user  
                           ^  
  symbol:   variable test  
  location: class stringy  
5 errors  

Tool completed with exit code 1

【问题讨论】:

  • 使用userInput 而不是scan。例如:String webPath = userInput.next();
  • 您的扫描仪被命名为userInput 而不是scan.... 测试变量设置在哪里?
  • 请记住,大多数开发人员使用 IDE,它通常会在您尝试运行代码之前突出显示这些错误
  • @cricket_007 - 我会检查的。这可能是问题所在!

标签: java symbols


【解决方案1】:

您的代码中有两个问题:

第一

您已指定扫描仪使用此行: Scanner userInput = new Scanner(System.in);

所以,你应该在这一行使用userInput而不是scan:String webPath = scan.next(); like String userInput = scan.next();

第二

您在使用前没有设置测试变量,所以在使用前分配测试变量。

【讨论】:

  • 成功了,谢谢!最终也跳过布尔值并仅使用“webPath.contains(“https://”);”在“IF”语句中
猜你喜欢
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多