gpt4 book ai didi

java - 使用数组调用方法时出现“.class”预期错误

转载 作者:行者123 更新时间:2023-12-01 23:54:14 25 4
gpt4 key购买 nike

到目前为止,我已经买了三本书并观看了几个视频,但我仍然不清楚为什么它不能编译。任何帮助将不胜感激。我正在尝试读取信用卡号码文件,并根据用户在主文件中输入的号码进行检查。这是我的类(class),然后是我的主要类(class):(感谢您的帮助)

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

public class Validator1
{
int[] valid;

public Validator1()throws IOException
{

}

public Validator1(String fileName)throws IOException
{
int beginning = 0;
int place = 0;
int size = 0;

File file = new File(fileName);
Scanner inputFile = new Scanner(file);
Scanner inputFile2 = new Scanner(file);

while(inputFile.hasNextInt())
{
size++;
inputFile2.nextInt();
}


while(inputFile2.hasNextInt() && place < valid.length)
{
valid[place] = inputFile2.nextInt();
place++;
}

}

public void sort(int[]valid)
{
valid = new int[valid.length];
for(int start = 0; start < valid.length; start++)
{
int lowestValue = valid[start];
int lowestIndex = start;
for(int i = start + 1; i < valid.length; i++)
{
if(valid[i] < lowestValue)
{
lowestValue = valid[i];
lowestIndex = i;
}
}
int temp = valid[start];
valid[start] = valid[lowestIndex];
valid[lowestIndex] = temp;
}
}

public boolean isValid(int[] valid, int number)
{
int low =0;
int high = valid.length-1;
while(high >= low)
{
int middle = (high + low)/2;
if(valid[middle] == number)
{
return true;
}
if (valid[middle] < number)
{
low = middle +1;
}
if(valid[middle] > number)
{
high = middle-1;
}
}
return false;
}
}




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

public class ChargeIt1
{
public static void main(String[] args)
{
int accountNumber;
int[] valid = 0;
Scanner keyboard = new Scanner(System.in);



System.out.println("Enter the file name: ");
String fileName1 = keyboard.nextLine();
String fileName = fileName1 + ".txt";

File file = new File(fileName);

while(!file.exists())
{
System.out.println("the file does not exist");
System.out.println("Enter the file name: ");
fileName1 = keyboard.nextLine();
fileName = fileName1 + ".txt";
file = new File(fileName);

}
System.out.println("Enter your charge account number: ");
accountNumber = keyboard.nextInt();

//Validator1 val = new Validator1(fileName);
try
{
Validator1 val = new Validator1(fileName);
if(val.isValid(valid, accountNumber))
System.out.println("That is a valid account number.");
else
System.out.println("That is an INVALID account number.");

}
catch(Exception e)
{
//System.out.println("An error has occurred.");
e.printStackTrace();
}

}
}

最佳答案

这一行是错误的:

int[] valid = 0;

0 不是数组。

关于java - 使用数组调用方法时出现“.class”预期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15855889/

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