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. あなたがファイルを1行ずつ読んでいるように、あなたのwhileでは以下を使用してください。

    while (fileScan.hasNextLine()){
    
  2. nextLine() メソッドのみを使用する

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

これらがあなたの問題を解決することを願っています。

于 2013-03-14T22:19:25.240 に答える