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