java - 不确定如何将用户的输入(字符串)转换为可接受的数据类型

标签 java

我有四个文件:

LibraryPatron.java

/** A class that maintains a shopping cart for an online store.
@author Frank M. Carrano
@version 4.0
*/ // Data fields are: Name, Library card number, Street, City, Zip
import java.util.*;

public class LibraryPatron
{
public static void main(String[] args)
{


Patron[] Patron =
   {
       new Patron("Name", "Library Card", "Address", "City", "Zip Code"),

new Patron("Name", "Library Card", "Address", "City", "Zip Code")};

LinkedInterface libraryPatronList = new LinkedBag();

Scanner input = new Scanner(System.in);

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

if (libraryPatronList.contains(name))
{   
System.out.println ("Patron is in system.");
String info = libraryPatronList.getData(name);

for (String field: info.split(","))
System.out.println(field);
}

else
System.out.println ("Patron is not in system.");
}
}

Patron.java

/** A class of items for sale.
@author Frank M. Carrano
@version 4.0
*/

public class Patron // Data fields are: Name, Library card number, Street, City, Zip
{
private String name;
private String libraryCard;
private String street;
private String city;
private String zip;

public Patron(String nameOfPatron, String libraryCardNumber, String streetName, String cityName, String zipName)
{
name = nameOfPatron;
libraryCard = libraryCardNumber;
street = streetName;
city = cityName;
zip = zipName;
} // end constructor


public String getName()
{
return name;
} // end getDescription

@Override

public boolean equals (Object patron2)
{
Patron tempPatron;
tempPatron = (Patron) patron2;
return (name.equals (tempPatron.getName()));
}

public String toString()
{
   return name + "," + libraryCard + "," + street + "," + city + "," + zip;

} // end toString
} // end Item

LinkedBag.java

public final class LinkedBag implements LinkedInterface
{
Node firstNode;
int numberOfEntries;


// Simple constructor
public LinkedBag()
{
firstNode = null;
numberOfEntries = 0;
} // end default constructor


/** Locates a given entry within this bag.
@param anEntry The entry to be located.
@return A reference to the node if located, or null otherwise. */
private Node getReferenceTo (T anEntry)
{
Node currentNode = firstNode;
boolean found = false;
while (!found && currentNode != null)
{
if (anEntry.equals (currentNode.data))
found = true;
else
currentNode = currentNode.next;
}
return currentNode;
}


/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if the bag contains anEntry, or false otherwise. */
public boolean contains(T anEntry)
{
Node currentNode = getReferenceTo (anEntry);
return !(currentNode == null);
} // end


/** Retrieves all entries that are in this bag.
@return A newly allocated array of all the entries in this bag. */
public T[] toArray()
{
@SuppressWarnings ("unchecked")
T[] result = (T[]) new Object[numberOfEntries];
int index = 0;
Node currentNode = firstNode;
while (currentNode != null)
{
result [index] = currentNode.data;
++index;
currentNode = currentNode.next;
}
return result;
}

private class Node
{
private T data;
private Node next;

// Two contructors.
private Node (T dataPortion)
{
this (dataPortion, null);
}

private Node (T dataPortion, Node nextNode)
{
data = dataPortion;
next = nextNode;   
}
}

/** Returns information in node containing anEntry in String form
* @param anEntry The entry to be searched for.
* @return String value of data in node if found, or empty string otherwise */

public String getData(T anEntry)
{
   String result = "";

   Node currentNode = getReferenceTo (anEntry);
   if (currentNode != null)
   {
       result = currentNode.data.toString();
       }
   return result;
   } // end getData

} // end LinkedBag

LinkedInterface.java

/**
An interface that describes the operations of a bag of objects.
@author Frank M. Carrano
@version 4.0
*/
public interface LinkedInterface
{

   public boolean contains(T name);

   public String getData(T anEntry);
} // end BagInterface

我的问题是关于我迄今为止拥有的 LibraryPatron.java 代码。

我正在尝试获取用户的输入并扫描我创建的订阅者列表,以验证用户的输入是否是我的链接列表的一部分,但我不知道如何从字符串转换并使用这些方法我需要。如果用户的输入是我提前创建的链接列表的一部分,它将返回用户的信息,包括他们的姓名、借书证号、地址、城市和邮政编码。任何建议或指导将不胜感激,谢谢!

最佳答案

  1. LinkedBag 类没有添加某些内容的方法。 您必须向 LinkedInterface 和 LinkedBag 添加代码才能执行此操作。类似于

    public void add(T anEntity)

  2. 用您创建的订阅成员(member)填充 linkedBag。

  3. LibraryPatron 中的
  4. name 未定义。那必须改变成这样 扫描仪输入 = new Scanner(System.in); 字符串名称 = 输入。 nextLine();

关于java - 不确定如何将用户的输入(字符串)转换为可接受的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60501151/

相关文章:

java - 文本框和标签问题

java - 方法局部内部类不能使用方法内声明的变量

java - 如何在 Java 中维护唯一列表?

java - Spring JNDI-查找三元运算符

Java Swing 问题,为什么changeEvent 使用jButton 触发额外的事件?

java - 在 Spring MVC 中实现 XSLT View

java - root=这样带值的url,带key不带值的参数如何处理?

java - maven-gae-plugin,如何将我的类打包为 JAR?

java - PHP 在 Ubuntu 上运行 jar 文件

java - 如何在没有正则表达式的情况下解析字符串