지금까지 세 권의 책을 구입했으며 여러 개의 비디오를 시청했는데 왜 컴파일되지 않는지 아직 확실하지 않습니다. 어떤 도움이라도 대단히 감사하겠습니다. 나는 신용 카드 번호의 파일을 읽고 사용자가 내 메인에 입력 한 번호를 확인하려고합니다. 필자가 메인에서 isValid 메소드를 호출하면 배열을 인수로 사용해야한다는 것을 확신하지만, '.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;
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();
}
}
}
생성자는 초기화되지 않은 배열을 처리합니다. – chris
컴파일러는 무엇을 말합니까? 컴파일러 출력이 있습니까? – A4L