gpt4 book ai didi

java - 找不到符号构造函数 Scanner(java.lang.String[]) 错误?

转载 作者:行者123 更新时间:2023-12-02 05:08:01 32 4
gpt4 key购买 nike

该程序的主要目的是能够从浏览器读取请求行并打印出该特定请求行的前两个单词。然而,当涉及到编译程序时,我一次又一次地遇到相同的错误,我不确定问题是什么。这是我编写的代码示例:

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

public class UselessHTTPServer05 {
public static void main(String args[]) throws Exception {
int port = Integer.parseInt(args[0]);
ServerSocket serverSock = new ServerSocket(port);

while (true) {
Socket conn = serverSock.accept();
Scanner scanin = new Scanner(conn.getInputStream());
String line = null;
int nlines=0;
String[] stringArray = new String[32];

while (true) {
line = scanin.nextLine();
if (line.length() == 0)
break;
stringArray [nlines] = line;
nlines = nlines + 1;
//System.out.println("line "+nlines+": " + line);

for (int i = 0; i < nlines; i++)
System.out.println("Line: " + i + " " + stringArray[i]);

Scanner scans = new Scanner(stringArray);
String command = scans.next();
String resource = scans.next();
System.out.println("Command: " + command);
System.out.println("Resource " + resource);
}

String reply="HTTP/1.0 404 Not Found\r\n" +
"Connection: close\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<h1>Sorry, work in progress</h1>\r\n";

OutputStream outs = conn.getOutputStream();
outs.write(reply.getBytes());
conn.close();
}
}
}

该错误与代码的扫描器部分有关,其中我声明了一个名为“Scans”的扫描器,并编写了代码来执行扫描请求行并打印出存储在中的前两个单词的任务“命令”和“资源”。

最佳答案

String[] stringArray = new String[32]; 
...
Scanner scans = new Scanner(stringArray);

stringArray 的类型为 String[],访问 Scanner用于验证没有此类可用构造函数的 API。

关于java - 找不到符号构造函数 Scanner(java.lang.String[]) 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27603677/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com