好吧,我已经输入了一些文本文件的记录,我可以写入和读取此文件,但我现在正在尝试搜索此文本文件,并遇到了问题.
package assignmentnew;
// Import io so we can use file objects
import java.io.*;
import java.util.Scanner;
public class SearchProp {
public void Search() throws FileNotFoundException {
try {
String details, input, id, line;
int count;
Scanner user = new Scanner(System.in);
System.out.println();
System.out.println();
System.out.println("Please enter your housenumber: ");
input = user.next();
Scanner housenumber = new Scanner(new File("writeto.txt"));
while (housenumber.hasNext())
{
id = housenumber.next();
line = housenumber.nextLine();
if (input.equals(id))
{
System.out.println("House number is: " + id + "and" + line);
break;
} …Run Code Online (Sandbox Code Playgroud) 好的,所以我在控制台中使用扫描仪创建了一个搜索功能,但我现在想从我的GUI创建一个搜索功能.当文本输入a JTextField并被JButton单击时,我想要一种方法,它将逐行搜索我的文本文件,直到找到搜索的条件并将其打印到a JOptionPane.
文本文件中的数据格式如下:
什么是最好的方式去做?
提前致谢
我已经创建了两个类Standard,Family并扩展了抽象类Room,在编译Standard类时我遇到了错误"在调用超类型构造函数之前无法引用roomNumber",但我无法理解为什么,任何帮助都将不胜感激.
Room
public abstract class Room
{
public int roomNumber;
public String roomType;
public boolean ensuite;
public boolean available;
public Room(int roomNumber, boolean enSuite)
{
this.roomNumber = roomNumber;
ensuite = enSuite;
available = false;
}
}
Run Code Online (Sandbox Code Playgroud)
Standard
public class Standard extends Room
{
private int roomNumber;
private int childCap;
private int adultCap;
public Standard(int theNumber, int kidsCap, int adultsCap)
{
super(theNumber, roomNumber);
childCap = childsCap;
adultCap = AdultsCap;
}
}
Run Code Online (Sandbox Code Playgroud) 我想要做的是忽略数组中的所有对象并将其指向另一组对象.像这样的东西:
[myArrayFullOfObjects removeAllObjects];
myArrayFullOfObjects = nil;
NSArray *newArray = [[NSArray alloc] initWithObjects:obj1, obj2, nil];
myArrayFullOfObjects = newArray;
Run Code Online (Sandbox Code Playgroud)
这是对的吗?我希望避免使用旧的对象"myArrayFullOfObjects"指向的内存泄漏.
我正在使用ElementTree从xml文件中的特定标记中成功提取值.如果不存在必需的标记,我想添加一些保护级别,我想抛出异常.
我发现ElementTree返回值"None"来代替缺少的标记.但是我甚至无法让那个工作对我有利:(
value = xmlTree.findtext('tag')
print value
Run Code Online (Sandbox Code Playgroud)
如果标签"tag"不存在,将打印"None".
所以我试过了
if value == "None":
print "tag not present"
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,它不会抓住它?
我的下一个想法是看看ElementTree是否有异常构建,可以检测到丢失的标签,但也没有运气.
我想在线性布局上旋转png图像.我的图像是半圆形的,上面有不同的颜色.
有任何想法吗?
用户从选择框中选择一个值,然后通过表单传递给另一个页面,该页面应该显示该选择的mysql记录.这没有显示任何结果,但是值肯定会被传递,因为它可以被回显.
<?php
include("top.php");
$db = getConnection();
$eventID = $_POST['eventID'];
echo $eventID;
$queryEvent = $db->query("SELECT * FROM projectEvent WHERE eventID = '$eventID'");
$queryEvent->setFetchMode(PDO::FETCH_ASSOC);
$record = $queryEvent->fetchALL();
?>
<form name="form1" id="form1" method="post" action="deleteOpen.php">
<p> are you sure you want to delete the following Open Day? </p>
<table width="200" cellpadding="0" cellspacing="0">
<tr>
<th scope="row">eventID</th>
<td><?php echo $record -> eventID; ?></td>
</tr>
<tr>
<th scope="row">propertyID</th>
<td><?php echo $record -> propertyID; ?></td>
</tr>
<tr>
<th scope="row">eventDate</th>
<td><?php echo $record -> eventDate; ?></td>
</tr>
<tr>
<th …Run Code Online (Sandbox Code Playgroud) 我试图获取文本字段的值,并将其应用于方法,以便在文本文件中搜索该值.但在我的方法中,我显示了一个Missing Return Value错误,似乎无法使其正常工作.下面是我的代码:
submitsearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
whatToSearch = searchfield.getText();
result = SearchString(whatToSearch);
}
});
}
public String SearchString(String result)
{
String input;
try{
String details, id, line;
int count;
Scanner housenumber = new Scanner(new File("writeto.txt"));
while(housenumber.hasNext())
{
id = housenumber.next();
line = housenumber.nextLine();
{
if(!housenumber.hasNext())
JOptionPane.showMessageDialog(null,"No Properties with this criteria");
}
if(result.equals(id));
{
JOptionPane.showMessageDialog(null,id + line );
}
}
}
catch(IOException e)
{
System.out.print("File failure");
}
}
}
Run Code Online (Sandbox Code Playgroud)
附录:
在SearchString中,我希望在我的文本文件中搜索在我的文本字段中输入的值,将其显示在JOptionPane中.虽然我点击搜索时现在有了返回语句,但无论是否与我的搜索匹配,我都会逐一显示JOptionPanes中的所有记录
我有类的ArrayList Room这是在课堂上举行Hostel,我想此ArrayList写入一个文本文件中.这样做最有效的方法是什么?
宿舍楼
public class Hostel
{
private ArrayList < Room > rooms;
}
Run Code Online (Sandbox Code Playgroud)
房间等级
abstract class Room
{
public Room(int newRoomNo, boolean newRoomEnSuite, int newRoomNights, String newRoomBooker)
{
roomNo = newRoomNo;
roomEnSuite = newRoomEnSuite;
roomBooking = "Booked";
roomNights = newRoomNights;
roomBooker = newRoomBooker;
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个Hostel类和一个Room类,Hostel该类包含一个ArrayList rooms,这个Arraylist中的一个值是Boolean available.在Hostel类中,我创建了一个if语句,它应该显示ArrayList中boolean available为true的任何条目,但它显示所有值,包括那些available为false但现在已经更改为true的值.任何人都可以告诉我哪里出错了.
public Room showAvail()
{
String theString = "Available Rooms";
if (Room.available == true)
for (Room room : rooms)
{
theString = theString + room.getRoomData() + "\n";
System.out.println(theString);
}
return null;
}
Run Code Online (Sandbox Code Playgroud) java ×6
search ×2
swing ×2
text-files ×2
android ×1
arraylist ×1
elementtree ×1
forms ×1
if-statement ×1
ios ×1
mysql ×1
php ×1
python ×1
super ×1
writetofile ×1
xcode ×1