2014-07-25 1 views
1

Pour ce code, il s'agit d'un programme de formatage de texte java. Je l'exécute et puis il demande la taille de la colonne, mais quand il demande le fichier, je le tape et il dit qu'il n'existe pas?Pourquoi ne puis-je pas ouvrir le fichier d'entrée à l'invite?

Est-ce que je fais quelque chose de mal?

J'ai essayé de faire l'emplacement du fichier, puis le nom du fichier lui-même. Devrais-je essayer de réenregistrer sur le même lieu de travail? S'il vous plaît aider.

Voici mon code, s'il y a plus de problèmes internes que je néglige. Je pensais que ça fonctionnait jusqu'à maintenant.

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

/** 
* Formatter - simple text formatting 
*/ 
public class Formatter 
{ 
public static void exit(Scanner sc) 
{ 
    // Keep console window alive until 'enter' pressed (if needed). 
    System.out.println(); 
    System.out.println("Done - press enter key to end program"); 
    String junk = sc.nextLine(); 
    System.exit(0); 
} 

/** 
* main - text formatting 
*/ 
public static void main (String[] args) 
{ 
    Scanner sc = new Scanner(System.in); 

    // Get maximum output column width for formatting 
    int maxWidth; 
    System.out.println("Enter the output column width"); 
    do { 
     maxWidth = sc.nextInt(); 
     sc.nextLine(); 
     if (maxWidth < 30 || maxWidth > 100) { 
      System.out.println("Column size must be between 30 and 100...re-enter"); 
      maxWidth = 0; 
     } 
    } while (0 == maxWidth); 


    // Get name of input text file to read and format, check it exists, and readable. 



    File textinFile = null; 
    Scanner scan = null; 
    String read = null; 
    String read1 = ""; 
    String textinName; 

    do { 
     System.out.println("Enter the name of the input text file"); 

     textinName = sc.nextLine(); 

     textinFile = new File(textinName); 
     if (!textinFile.exists()) { 
      System.out.println("File does not exist: " + textinName + " - re-enter"); 
      textinName = null; 
      continue; 
     } 
     if (!textinFile.canRead()) { 
      System.out.println("Unable to read from file: " + textinName + " - re-enter"); 
      textinName = null; 
      continue; 
     } 


     try { 
      scan = new Scanner(textinFile); 
      scan = new Scanner(new File(textinName)); 
      while(scan.hasNextLine()){ 
      read = scan.nextLine() + "\n"; 
      read1 += read; 

      } 
      scan.close(); 
     } catch (FileNotFoundException e) { 
      // Unexpected, as already checked for file existing 
      System.out.println("Unexpected error: " + e.toString()); 
      //textinScanner = new Scanner(textinFile); 


      continue; 
     } 

    } while (!textinFile.exists()); 


    // Get name of the output file to write formatted text to, and open a file. 

    String textoutName = null; 
    do { 
     System.out.println("Enter the name of the output file"); 
     textoutName = sc.nextLine(); 

     File textoutFile = new File(textoutName); 
     if (textoutFile.exists()) { 
      System.out.println("File already exists: " + textoutName); 
      System.out.println("Do you want to overwrite this file (Y/N)"); 
      String yesno = sc.nextLine(); 
      if (yesno.toLowerCase().startsWith("n")) { 
       System.out.println("Re-enter output file name"); 
       textoutName = null; 
       continue; 
      } 
     } 
    } while (null == textoutName); 

    // Open the output file for writing (or the console). 
    PrintWriter textoutWriter = null; 
    Scanner outputReader = null; 
    String newReader = null; 
    String newReader1 = ""; 
    try { 
      //outputReader = new Scanner(new File(textinName)); 
      //textoutWriter = new PrintWriter(new BufferedWriter(new FileWriter(textoutName))); 

     scan = new Scanner(textinFile); 
     scan = new Scanner(new File(textinName)); 
     while(scan.hasNext()){ 
      newReader = scan.next() + "\n"; 
      newReader1 += newReader; 

      } 
      scan.close(); 
    } catch (IOException e) { 
     System.out.println(e.toString()); 
     exit(sc); 
    } 


    System.out.println("\nGiven the following input file:\n"); 

    System.out.println(read1); 

    System.out.println("\nSpecifying a formatted output width of " + maxWidth + " should produce the following output:\n"); 
    System.out.println("Formatted output text follows...\n"); 

    for(int n = 0; n <= maxWidth; n++) 
    { 
     System.out.print("*"); 
    } 

    // Read words from the input file, appending to the current line being built until 
    // the line plus the newest word would exceed the output column width. 
    // When the line is full, write it to the output file, and reset it to empty. 
    // Continue until end of file encountered. 
    String word = ""; 
    String line = ""; 

    System.out.println(""); 

System.out.println(newReader1); 

} 

}

+0

Sur quel système d'exploitation êtes-vous? Quel est le nom du fichier que vous tapez? Est-ce que le fichier existe vraiment si vous l'ouvrez dans l'explorateur/finder/what? D'où vient le message - de la vérification 'textinFile.exists()' ou du bloc 'catch (FileNotFoundException e)'? –

Répondre

0

Scanner.nextLine() comprend la ENTER à la fin de la ligne. Vous devez supprimer ce personnage, alors ça devrait marcher.

Questions connexes