java - 用户创建了链接列表类来将对象存储在具有菜单功能的文本文件中

标签 java linked-list menu

我一直在尝试使用我自己的链接列表类(LList)来帮助将住宅对象存储在房地产类中,该类具有我从关联数据文件中提取并存储在关联数据文件中的住宅对象的链接列表。我在 RealEstate 类中实现菜单时遇到问题,该菜单允许用户与链接列表进行交互,从而对关联的数据文件进行更改。由于某种原因,当我在尝试显示菜单后尝试请求用户输入时,我的程序似乎挂起。任何帮助将不胜感激!

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileReader;
import java.io.Console;
import java.io.DataInputStream;

public class RealEstate2
{
    LList<Dwelling> dList= new LList<Dwelling>();
    int ch;
    FileReader w = null;
    String line1=null;
    String line2=null;
    String line3=null;
    double foot = 0;
    double val = 0;
    String dwellings = null;
    private String fileName;
    //int choice = 1;



    //constructor
    public RealEstate2(String m) throws FileNotFoundException, ListException
    {
        fileName = m;
        //File data = new File(fileName);
        Scanner sc = new Scanner(new File(fileName));
        try
        {
        while (sc.hasNextLine())
            {
            line1 = sc.nextLine();
            line2 = sc.nextLine();
            line3 = sc.nextLine();
            foot = Double.parseDouble(line2);
            val = Double.parseDouble(line3);
            dList.add(new Dwelling(line1,foot,val));
            }
        }
        catch(ListException a)
                {
                 throw new ListException("fuck");
                }
            sc.close;

        //System.out.println(dList);
    }//end constructor

    public void Manage() throws IOException
        {
        Scanner in = new Scanner(System.in);
        //Console console = System.console();
        System.out.println("1. Display items");
        System.out.println("2. Add item");
        System.out.println("3. Insert item");
        System.out.println("4. Delete item");
        System.out.println("5. Exit");

        System.out.println("Enter option:");

        int choice = Integer.parseInt(System.console().readLine());
        //choice = in.nextInt();

        try
        {
        //display
        if (choice == 1)
            System.out.println(this);
        //add
        if (choice == 2)
            System.out.println("Please enter the address:");
            String a = sc.nextLine();
            System.out.println("Please enter the square footage:");
            double f = sc.nextDouble();
            System.out.println("Please enter the value:");
            double v = sc.nextDouble();
            Dwelling x = new Dwelling(a,f,v);
            hList.add(x);
            //
        //insert
        if (choice == 3)
            System.out.println("Please enter the address:");
            String ad = sc.nextLine();
            System.out.println("Please enter the square footage:");
            double fo = sc.nextDouble();
            System.out.println("Please enter the value:");
            double va = sc.nextDouble();
            Dwelling y = new Dwelling(ad,fo,va);
            System.out.println("Please enter the position to insert:");
            int p = sc.nextInt();
            hList.insert(y,p);
        //delete
        if (choice == 4)
            System.out.println("Please enter the position to delete:");
            int d = sc.nextInt();
            hList.delete(d);
        //exit
        if (choice == 5)
            return;

        }
        catch(ListException a)
        {
            System.out.println(a);
        }
 //use update method to update data file

        }//end of manage method














        //tostring

    public void update() throws IOException
        {
            try
            {
            FileWriter d = new FileWriter(fileName);
            dwellings = dList.toString();
            for (int i = 0; i < dwellings.length(); i++)
                d.write(dwellings.charAt(i));
                d.close();
           }
           catch (IOException a)
           {
               throw new IOException();
           }
        }//end of update method


}//end of class

public class LList<T> //implements ListInterface<T>
{
    private Node<T> head;
    private Node<T> tail;
    private int counter;

    //constructor
    public LList( )
    {
        head=null;
        tail=null;
        counter=0;
    }


    //add method
    public void add( T m ) throws ListException
    {
        try
        {
            Node<T> temp = new Node<T>( );
            temp.setData( m );
            temp.setNext( null );

            if ( head == null)
            {
                head = temp;
            }
            else
            {
                tail.setNext( temp );
            }

            tail = temp;

            counter++;

        }
        catch(OutOfMemoryError e)
        {
            throw new ListException("Cannot add. No more memory");
        }
    }

    //delete method
    public T delete(int position) throws ListException
    {
        Node<T> current=head;
        Node<T> before=null;

        T temp;

        if ( counter == 0 )
            throw new ListException("Cannot delete. List is empty.");

        if ( position >= 1 && position <= counter )
        {
            //the position is valid
            if (counter == 1 ) //is there only 1 node
            {
                temp = head.getData();
                head=null;
                tail=null;
                counter--;
                return temp;
            }
            else
            {
                //there are at least 2 nodes
                if ( position == 1 )
                {
                    //there are at least 2 nodes and the user is trying to delete the first node
                    temp = head.getData();
                    head = head.getNext();
                    counter--;
                    return temp;
                }
                else
                {
                    //there are at least 2 nodes but the user is not trying to delete the first
                    int k = 1;

                    while (k != position )
                    {
                        before = current;
                        current = current.getNext();
                        k++;
                    }
                    //we have arrived at the node to be deleted
                    temp = current.getData();
                    before.setNext( current.getNext() ); //gets rid of desired node

                    //Did we just delete last node
                    if ( before.getNext() == null )
                        tail=before;
                    counter--;
                    return temp;

                }


            }
        }
        else
        {
            //the position is invalid
            throw new ListException("Cannot delete. Position is bad.");
        }





    }//end of delete method

    //tostring method
    public String toString()
    {
        if (head == null )
        {
            return "The list is empty.";
        }
        String t = "";
        Node<T> temp;
        temp=head;
        while ( temp != null )
        {
            t += temp.getData() + "\n";

            temp = temp.getNext();
        }
        return t;
    }//end of toString

    //insert method
    public void insert( T item, int position ) throws ListException
    {
        try
        {
            if (counter == 0 )
                throw new ListException("Cannot insert. List is empty.");

            if (item == null )
                throw new ListException("Cannot insert. Item is invalid.");

            if (position <1 || position > counter )
                throw new ListException("Cannot insert. Position is bad.");

            Node<T> temp = new Node<T>();
            temp.setData( item );

            if (position == 1)
            {
                temp.setNext( head );
                head = temp;
            }
            else
            {
                Node<T> before = head;
                Node<T> current = head;
                int k=1;
                while( k != position )
                {
                    before = current;
                    current = current.getNext();
                    k++;
                }

                temp.setNext( current );
                before.setNext( temp );
            }
        }
        catch(OutOfMemoryError e)
        {
            throw new ListException("Cannot insert. No more memory.");
        }
    }//end of insert method

    public int Size( )
    {
        return counter;
    }
} //end of LList

public class Node<T>
{
    private T data;
    private Node<T> next;

    //default constructor
    public Node()
    {
        data = null;
        next = null;
    }

    public void setData( T  p )
    {
        data = p;

    }

    public T getData()
    {
        return data;
    }

    public void setNext( Node<T> n)
    {
        next = n;
    }

    public Node<T> getNext()
    {
        return next;
    }

} //end of the class
public class Dwelling
{
    private String address;
    private double footage;
    private double value;

    //constructor
    Dwelling(String add, double f, double v) throws ListException
    {
        if (add == null || add.length() == 0)
                {
                    throw new ListException("The address cannot be null");
                }
                else
                {
                    this.address = add;
                }
        if(f <= 100)
                    {
                        throw new ListException("The squarefootage cannot be less then 100 ft.");
                    }
                    else
                    {
                        this.footage = f;
                    }
        if(v <= 100000)
                    {
                        throw new ListException("The value cannot be less then $100000.");
                    }
                    else
                    {
                        this.value = v;
                    }
    }


      // Getter
      public String getAddress()
      {
        return address;
      }

      // Setter
      public void setAddress(String newAddress) throws ListException
      {
        if (newAddress == null || newAddress.length() == 0)
        {
            throw new ListException("The address cannot be null");
        }
        else
        {
            this.address = newAddress;
        }

      }

      // Getter
      public double getFootage()
      {
        return this.footage;
      }

      // Setter
      public void setFootage(double foot) throws ListException
      {
          if(foot <= 100)
            {
                throw new ListException("The squarefootage cannot be less then 100 ft.");
            }
            else
            {
                this.footage = foot;
            }

      }

    // Getter
      public double getValue()
      {
      return this.value;
      }

  // Setter
      public void setValue(double val) throws ListException
      {
            if(val <= 100000)
                {
                    throw new ListException("The value cannot be less then $100000.");
                }
            else
                {
                     this.value = val;
                }




      }

      //toString
      public String toString()
      {
            return address + "\n" + footage + "\n" + value + "\n";
      }


}

最佳答案

您的 if block 缺少其周围的右括号 {}。为了清晰起见,即使 if block 只有一行,也始终放置一个括号。
另外,if block 实际上应该是一个 switch block ,但请记住始终为 switch 中的每个选择添加一个中断。

您在构造函数中声明 Scanner sc 并尝试在 Manage 方法上使用它,我不确定它是如何编译的。
在您声明扫描仪的管理方法中,您可能应该使用该扫描仪变量来扫描菜单选项中的值。
另外,由于某种原因您正在使用

int choice = Integer.parseInt(System.console().readLine()); 

要读取用户选择,您应该使用扫描仪。

int choice = in.nextInt();

编辑:这是重写的 if block :

    switch (choice) {
        case 1:
            System.out.println(this);
            break;

        //add
        case 2:
            System.out.println("Please enter the address:");
            String a = in.nextLine();
            System.out.println("Please enter the square footage:");
            double f = in.nextDouble();
            System.out.println("Please enter the value:");
            double v = in.nextDouble();
            Dwelling x = new Dwelling(a, f, v);
            hList.add(x);
            break;
        //
        //insert
        case 3:
            System.out.println("Please enter the address:");
            String ad = in.nextLine();
            System.out.println("Please enter the square footage:");
            double fo = in.nextDouble();
            System.out.println("Please enter the value:");
            double va = in.nextDouble();
            Dwelling y = new Dwelling(ad, fo, va);
            System.out.println("Please enter the position to insert:");
            int p = sc.nextInt();
            hList.insert(y, p);
            break;

        //delete
        case 4:
            System.out.println("Please enter the position to delete:");
            int d = in.nextInt();
            hList.delete(d);
            break;

        //exit
        case 5:
            return;
    }    

关于java - 用户创建了链接列表类来将对象存储在具有菜单功能的文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61985233/

相关文章:

c# - WPF:更改模板的背景颜色

arrays - bash 列出文件夹中的所有子目录,将它们写入数组以在菜单中使用

java - 通过 Jenkins 测试失败时如何存储 Selenium 屏幕截图

java - 查找字符串中的整数个数(不是数字)

data-structures - 为什么链表几乎总是与单独的链一起使用?

c++ - 在 C++ 中使用临时变量将节点添加到链表的末尾

javascript - 将第三级添加到 CSS 菜单

java - 格式化包含许多字符串和整数的长 system.out.print

java - 使用未知数量的 AND 进行 SQL 查询

c - 链表 - 段错误