扫描仪回车溢出

时间:2013-03-14 21:18:19

标签: java loops runtime-error java.util.scanner stack-overflow

如果有人能看到我去过的地方在我的代码中犯了一个错误,我将永远感激不尽。我知道这是一个淫秽的代码,但是在过去的几天里我一直在用它拉头发而根本无法理解它是怎么做的。我在课堂上向别人求助,但他们看不出我出错的地方。这与回车扫描仪问题有关。

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)

任何想法都会非常感激。

乔。

// ROOM CLASS

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

// HOUSE CLASS

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 个答案:

答案 0 :(得分:0)

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

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

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

    rms1 = fileScan.nextLine();
    yn = fileScan.nextLine();
    
  3. 我希望这些可以解决你的问题。

相关问题