0

如果有人能看到我的代码哪里出错了,我将永远感激不尽。我承认这是大量的代码,但在过去的几天里,我一直在用它来抓头发,根本无法理解如何处理它。我在课堂上向其他人寻求帮助,但他们看不到我哪里出错了。这与回车扫描仪问题有关。

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)

任何想法将不胜感激。

乔。

//房间类

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;
   }   
}

//家庭类

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;
   }   
}

//司机

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.");
  }
}   
4

1 回答 1

0

这可能是您没有正确读取文件的猜测。无论我从您的文件读取代码块和输入文件 "property.txt" 中看到什么,请进行以下更改。

  1. 在您使用以下内容时,您正在逐行读取文件。

    while (fileScan.hasNextLine()){
    
  2. 只使用 nextLine() 方法

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

我希望这些能解决你的问题。

于 2013-03-14T22:19:25.240 回答