J'essaie d'avoir une entrée d'utilisateur un interger, basé sur la valeur entière, j'appelle la fonction de mélange pour lire dans le contenu du fichier comme le montre le code ci-dessous. Je reçois, cette erreur:impossible de lire le fichier en Java
Project2.java:43: variable urlScan might not have been initialized
while (urlScan.hasNext())
^
Project2.java:34: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
fileScan = new Scanner (new File("input.txt"));
^
Des idées, ce que je pourrais faire mal ici?
import java.util.Scanner;
import java.io.*;
public class Project2
{
public static void main(String[] args)
{
System.out.println("Select an item from below: \n");
System.out.println("(1) Mix");
System.out.println("(2) Solve");
System.out.println("(3) Quit");
int input;
Scanner scan= new Scanner(System.in);
input = scan.nextInt();
System.out.println(input);
if(input==1) {
mix();
}
else{
System.out.println("this is exit");
}
}
public static void mix()
{
String url;
Scanner fileScan, urlScan;
fileScan = new Scanner (new File("input.txt"));
// Read and process each line of the file
while (fileScan.hasNext())
{
url = fileScan.nextLine();
System.out.println ("URL: " + url);
//urlScan = new Scanner (url);
//urlScan.useDelimiter("/");
// Print each part of the url
while (urlScan.hasNext())
System.out.println (" " + urlScan.next());
System.out.println();
}
}
}
Les erreurs sont assez claires ... – Reimeus
Avez-vous essayé Google? - [premier] (https://www.google.com/search?q=java+variable+might+not+have+been+initialized+stackoverflow), [second] (https://www.google.com/ recherche? q = java + non rapporté + exception + doit + être + attrapé + ou + déclaré + à + être + jeté + stackoverflow) – Dukeling