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는)

어떤 생각이 많이 주시면 감사하겠습니다.

조.

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

// 하우스 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; 
    } 
} 

// 드라이버

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

문제를 재현하기 위해해야 ​​할 일과 문제가 무엇인지에 대한 자세한 내용은 Google에서 도움을줍니다. – ajp15243

+1

아, "PropertyMenu' 클래스 (세 번째 코드 블록,"// DRIVER "아래)에서 길을 약 2/3 스크롤하면 오류가 발생한 섹션을 주석으로 구분하는 주석이 있습니다)입니다. – ajp15243

답변

0

이 올바르게 파일을 읽는하지 않는 것을 추측 할 수있다. 파일 읽기 코드와 입력 파일 "property.txt"의 블록에서 무엇을 보았더라도 다음과 같이 변경하십시오.

  1. 다음 단계는 파일을 한 줄씩 읽는 중입니다.

    while (fileScan.hasNextLine()){ 
    
  2. 만 나는이 문서가 문제를 해결하는 희망 꽵() 메소드를

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

를 사용합니다.