0

Si quelqu'un peut voir où je suis allé fait une erreur dans mon code, je serais éternellement reconnaissant. Je reconnais que c'est une quantité de code obscène, mais je me suis arraché les cheveux au cours des derniers jours et je n'arrive tout simplement pas à comprendre ce qu'il faut en faire. J'ai demandé de l'aide aux autres dans ma classe, mais ils ne peuvent pas voir où je me suis trompé. C'est à voir avec le problème du scanner de retour chariot.Retour chariot chariot Débordement

java.util.NoSuchElementException: No line found 
at java.util.Scanner.nextLine(Unknown Source) 
    at PropertyMenu.runMenu(PropertyMenu.java:109) 
    at PropertyMenu.main(PropertyMenu.java:7) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at 

edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand (JavacCompiler.java:272)

Toute pensée serait très apprécié.

Joe.

// CLASSE CHAMBRE

import java.util.Scanner; 
public class Room{ 

    private String description; 
    private double length; 
    private double width; 

    public Room (String description, double length, double width) { 
    this.description = description; 
    this.length = length; 
    this.width = width;  
    } 
    public Room(){ 
    Scanner scan = new Scanner(System.in); 
    scan.useDelimiter("\n"); 

    System.out.println("Enter description of room:"); 
    description = scan.next(); 

    System.out.println("Enter length of room:"); 
    length = scan.nextDouble(); 

    System.out.println("Enter width of room:"); 
    width = scan.nextDouble(); 
    } 

    public double getArea() { 
    return length*width; 
    } 

    @Override 
    public String toString() { 
    String result = ("***********************************\n"); 
      result +=("   Room Viewing   \n"); 
      result +=("************************************\n"); 
      result +=("The width of the room is " + width + "m.\n"); 
      result +=("the length of the room is " + length + "m.\n"); 
      result +=("the name of the room is: " + description +".\n"); 
    return result; 
    } 
} 

// MAISON DE CLASSE

import java.util.*; 
public class House { 
    private ArrayList<Room> abode; 
    private int idNum, numRooms; 
    private double totalArea; 
    private static int internalCount = 1; 
    private String address, roomInfo, houseType; 

    public House (String address, String houseType, int numRooms){ 
    System.out.println("THIS IS THE START OF MY 3 ARGUMENT CONSTRUCTOR"); 
    idNum = internalCount++; 
    this.address = address; 
    this.houseType = houseType; 
    this.numRooms = numRooms; 
    System.out.println("THIS IS THE END OF MY 3 ARGUMENT CONSTRUCTOR"); 
    } 
    public House (String address, String houseType, int numRooms, String roomInfo){ 
    System.out.println("THIS IS THE START OF MY 4 ARGUMENT CONSTRUCTOR"); 
    idNum = internalCount++; 
    this.address = address; 
    this.houseType = houseType; 
    this.numRooms = numRooms; 
    this.roomInfo = roomInfo; 

    Scanner scan = new Scanner(roomInfo); 

    String desc; 
    Double l; 
    Double w; 

    while (scan.hasNext()){ 
    desc= scan.next(); 
    l = Double.parseDouble(scan.next()); 
    System.out.println("the length from here"+l); 
    w = Double.parseDouble(scan.next()); 
    System.out.println("the width from here"+w); 

    new Room (desc,l,w); 
    } 
    System.out.println("THIS IS THE END OF MY 4 ARGUMENT CONSTRUCTOR"); 
    } 
    public void addRoom(){ 
    totalArea=0; 
    abode.add(new Room()); 
    for (int i=0; i<abode.size(); i++){ 
     totalArea += abode.get(i).getArea(); 
    } 
    } 
    public House() { 
    totalArea = 0; 
    abode = new ArrayList<Room>(); 
    idNum = ++internalCount; 
    Scanner scan = new Scanner(System.in); 
    scan.useDelimiter("\n"); 

    System.out.println("Enter address of house:"); 
    address = scan.next(); 

    System.out.println("Enter number of rooms:"); 
    numRooms = scan.nextInt(); 

    System.out.println("Enter type of house:"); 
    houseType = scan.next(); 

    for (int i=1; i<=numRooms; i++){ 
     addRoom(); 
    } 
    } 
    int getIdNum() { 
    return idNum; 
    } 

    @Override 
    public String toString() { 
     String result =("************************************\n"); 
      result +=("   House Viewing   \n"); 
      result +=("************************************\n"); 
      result +=("The house ID is " + idNum +".\n"); 
      result +=("The house address is " + address +".\n"); 
      result +=("The number of rooms here is " + numRooms +".\n"); 
      result +=("The house type is " + houseType +".\n"); 
      result +=("The total area of the house is " + totalArea +".\n"); 
      result +=(abode); 
    return result; 
    } 
} 

// DRIVER

import java.util.*; 
import java.io.*; 
public class PropertyMenu { 
    private ArrayList<House> properties =new ArrayList<House>(); 
    public static void main (String[] args) throws IOException{ 
    PropertyMenu menu = new PropertyMenu(); 
    menu.runMenu(); 
    } 

    public void runMenu() { 

    House h = null; 
    char selection = ' '; 
    Scanner s = new Scanner(System.in); 

    while (selection != 'e') { 
    System.out.println(); 
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
    System.out.println("Welcome to my Property database"); 
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
    System.out.println("What do you want to do?"); 
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
    System.out.println("To ADD a house enter......A"); 
    System.out.println("To VIEW a house enter.....V"); 
    System.out.println("To DELETE a house enter...D"); 
    System.out.println("To USE a file.............F"); 
    System.out.println("To QUIT enter.............E"); 
    selection = s.next().toLowerCase().charAt(0); 

     switch (selection) { 
     case 'a': 
      properties.add(new House()); 
      break; 
     case 'v': 
      System.out.println("Do you want to view all houses (y/n)?"); 
      String all = ""; 
      all = s.next(); 

      if (all.equalsIgnoreCase("y")){ 
      for (int i=0; i<properties.size(); i++){ 
       System.out.println("Property ID: "+ (properties.get(i))); 
      } 
      } 
      else if(all.equalsIgnoreCase("n")){ 
      System.out.println(""+ properties.size() +" houses have been created, choose the id of the house you wish to view.. (1/2/3 etc...)"); 
      System.out.println("List of property ID's: "); 
      for (int i=0; i<properties.size(); i++){ 
       System.out.println("Property ID: "+ (properties.get(i)).getIdNum()); 
      } 
      System.out.println("Enter ID of the house you wish to view:"); 
      int viewHouse = s.nextInt(); 
      if (viewHouse <= properties.size() && viewHouse >= 1){ 
       System.out.println(properties.get(viewHouse-1)); 
      } 
      else{ 
       System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
       System.out.println("  House Not Present  "); 
       System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
      } 
      } 
      else{ 
      System.out.println("Do you want to view all houses (y/n)?"); 
      all = s.next(); 
      } 
      break; 
     case 'd': 
      System.out.println(""+ properties.size() +" houses have been created, choose the id of the house you wish to delete.. (1/2/3 etc...)"); 
      System.out.println("List of property ID's: "); 
      for (int i=0; i<properties.size(); i++){ 
      System.out.println("Property ID: "+ (properties.get(i)).getIdNum()); 
      } 
      System.out.println("Enter ID of the house you wish to delete:"); 
      int deleteHouse = s.nextInt(); 
      if (deleteHouse <= properties.size() && deleteHouse >= 1){ 
      properties.remove(deleteHouse -1); 
      } 
      else{ 
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
      System.out.println("  House Not Present  "); 
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
      } 
      break; 
     case 'e': 
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
      System.out.println("   Goodbye    "); 
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
      break; 

//*********************************THIS IS WHERE MY PROBLEM IS, FROM HERE************* 

     case 'f': 
      try{ 
      Scanner fileScan = new Scanner (new File("property.txt")); 
      while (fileScan.hasNext()){ 
      System.out.println("THIS IS A FRESH LOOP"); 

      String a; 
      String ht; 
      String rms1; 
      int rms; 
      String yn; 
      String rmInfo; 

      a = fileScan.nextLine(); 
      System.out.println("ADDRESS"+a); 
      ht = fileScan.nextLine(); 
      System.out.println("HOUSE TYPE"+ht); 
      rms1 = fileScan.next(); 
      rms = Integer.parseInt(rms1); 
      System.out.println("HOUSEROOMs"+rms); 
      yn = fileScan.next(); 
      String overflow = fileScan.nextLine(); 
      System.out.println("Yes/no"+yn); 

      if (yn.equalsIgnoreCase("Y")){ 
       System.out.println("THIS IS THE START OF CHOICE = Y"); 
       rmInfo = fileScan.nextLine(); 
       properties.add(new House(a, ht, rms, rmInfo)); 
       System.out.println("THIS IS THE END OF CHOICE = Y"); 
      } 
      else{ 
       System.out.println("THIS IS THE START OF CHOICE = N"); 
       properties.add(new House(a, ht, rms)); 
       System.out.println("THIS IS THE END OF CHOICE = N"); 
      } 

      } 
     } 
      catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      } 
      break; 

//******************************************TO HERE^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

     default: 
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
      System.out.println("   Try again!   "); 
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
     } 
    } 
     System.out.println("Exiting program."); 
    } 
} 
+1

Où le problème se produit-il? –

+0

Plus de détails sur ce qu'il faut faire pour reproduire le problème, et quel est exactement le problème, nous aidera à vous aider. – ajp15243

+1

Ah, si l'on fait défiler environ 2/3 de la classe 'PropertyMenu' (le troisième bloc de code, ci-dessous" // DRIVER "), il y a des commentaires qui délimitent la section où l'erreur se produit (mais pas exactement) est). – ajp15243

Répondre

0

Ceci est peut être une supposition que vous ne lisez pas le fichier correctement. Tout ce que je vois à partir de votre bloc de code de lecture de fichier et le fichier d'entrée "property.txt", apporter les modifications suivantes.

  1. Dans votre temps, utilisez la suite, comme vous lisez le fichier ligne par ligne.

    while (fileScan.hasNextLine()){ 
    
  2. Utilisez uniquement une méthode

    rms1 = fileScan.nextLine(); 
    yn = fileScan.nextLine(); 
    

nextLine() J'espère que ces résoudra votre problème.