如何在无法接受String / char的情况下编写验证 [英] How to write validation where String/char cant be accepted
本文介绍了如何在无法接受String / char的情况下编写验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
展开 | 选择 | Wrap | 行号
推荐答案
import java.applet.Applet ;
import java.awt。*; //包含用于创建用户界面和绘制图形和图像的所有类。
import java.awt.event。*; //所有AWT事件的根事件类。
import java.lang.Object;
import java.util.EventObject;
import javax.swing。*; //提供一组轻量级 (所有Java语言)组件,尽可能在所有平台上工作相同。
公共类AirlineApplet扩展JApplet实现ActionListener {
JButton printPass;
JTextArea outputArea;
字符串输出;
int seatType,seatNum,choice;
boolean [] seatArray = {false,false,false,false,false,false,false,false,false,false};
public void init(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
chooseType();
printPass =新JButton(点击打印登机牌);
printPass.addActionListener(this);
outputArea = new JTextArea(40,40);
container.add(printPass);
container.add(outputArea);
}
public void chooseType(){
seatType = Integer.parseInt(JOptionPane.showInputDialog(" Plea se type 1 first smoking area or 2 for non smoking)区域));
selectSeat(seatType);
}
//
public void actionPerformed(ActionEvent event){
outputArea.append(output);
chooseType();
}
public void selectSeat(int z){
//参数在吸烟和非吸烟座位之间选择
if(z == 1)
{
if(seatArray [0]&& seatArray [1]&& seatArray [2]&& seatArray [3]&& seatArray [4])
{
choice = Integer.parseInt(JOptionPane.showInputDialog(所有吸烟区座位已被预订。您想要非吸烟区吗? 1表示是,2表示否。));
if(choice == 1)
selectSeat(2);
else
JOptionPane.showMessageDialog(null,谢谢,请再来。);
}
其他
{
//做吸烟座位并确保座位设置为无人居住
seatNum =(int)(Math.random()* 5) ;
if(!seatArray [seatNum])
seatArray [seatNum] = true;
else
selectSeat(z);
output ="你有座位 + seatNum +"吸烟区。 \ n" ;;
}
}
其他
{
if(z == 2)
if(seatArray [5]&& seatArray [6]&& seatArray [7]&& seatArray [8]& & seatArray [9])
JOptionPane.showMessageDialog(null,非吸烟区域没有座位。抱歉。);
else < br $>
{
//做非吸烟座位并确保座位无人居住
seatNum =(5 +(int)(数学) .random()* 5));
if(!seatArray [seatNum])
seatArray [seatNum] = true;
else
selectSeat(z);
output ="你有座位 + seatNum +"在非吸烟区。 \ n" ;;
}
}
}
}
我想问一下,当消息框询问用户他们想要什么类型的座位时我怎么能够进行验证,当用户键在其他字符而不是它应该显示的数字时向用户发出的信息是,不接受Char,仅接受号码。任何人都可以给我看代码,我将不胜感激。谢谢
Joseph
import java.applet.Applet;
import java.awt.*; // Contains all of the classes for creating user interfaces and for painting graphics and images.
import java.awt.event.*;//The root event class for all AWT events.
import java.lang.Object;
import java.util.EventObject;
import javax.swing.*;//Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.
public class AirlineApplet extends JApplet implements ActionListener {
JButton printPass;
JTextArea outputArea;
String output;
int seatType, seatNum, choice;
boolean[] seatArray = {false, false, false, false, false, false, false, false, false, false};
public void init() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
chooseType();
printPass = new JButton ("Click to print boarding pass");
printPass.addActionListener(this);
outputArea = new JTextArea(40, 40);
container.add(printPass);
container.add(outputArea);
}
public void chooseType() {
seatType = Integer.parseInt(JOptionPane.showInputDialog("Plea se type 1 first smoking area or 2 for non-smoking area"));
selectSeat(seatType);
}
//
public void actionPerformed(ActionEvent event) {
outputArea.append(output);
chooseType();
}
public void selectSeat(int z) {
//argument selects between the smoking and the non-smoking seat
if (z== 1)
{
if (seatArray[0] && seatArray[1] && seatArray[2] && seatArray[3] && seatArray[4])
{
choice = Integer.parseInt(JOptionPane.showInputDialog("All smoking section seat has been booked. Would you like non-smoking section? 1 for Yes, 2 for No."));
if (choice == 1)
selectSeat(2);
else
JOptionPane.showMessageDialog(null, "Thank you,Please Come Again.");
}
else
{
//do smoking seat and make sure that the seat is set to seat unoccupied
seatNum = (int)(Math.random()*5);
if (!seatArray[seatNum])
seatArray[seatNum] = true;
else
selectSeat(z);
output = "You have seat " + seatNum + " smoking area. \n";
}
}
else
{
if (z== 2)
if (seatArray[5] && seatArray[6] && seatArray[7] && seatArray[8] && seatArray[9])
JOptionPane.showMessageDialog(null, "There are no seats available in non-smoking area. Sorry.");
else
{
//do non smoking seat and make sure that the seat is seat to unoccupied
seatNum = (5 + (int)(Math.random()*5));
if (!seatArray[seatNum])
seatArray[seatNum] = true;
else
selectSeat(z);
output = "You have seat " + seatNum + " in non-smoking area. \n";
}
}
}
}
I wanna ask how am i able to do the validation when the message box out asking the user what type of seats they want, when user key in other character than a number it should display a message to user that Char are not accepted only number accepted. Anyone can show me the code, i would appreciate.Thank
Joseph
1.)发布代码时使用代码标签
2.)你可以使用Integer.parseInt解析String,如果String不是数字,那么将抛出异常。或者,您可以使用正则表达式。
1.) Use code tags when posting code
2.) You could parse the String using Integer.parseInt, if the String is not a number then an Exception will be thrown. Alternatively, you could use a regular expression.
1.)发布代码时使用代码标签
2 。)你可以使用Integer.parseInt解析String,如果String不是数字,那么将抛出异常。或者,您可以使用正则表达式。
1.) Use code tags when posting code
2.) You could parse the String using Integer.parseInt, if the String is not a number then an Exception will be thrown. Alternatively, you could use a regular expression.
感谢您的帮助,但我真的不明白您使用正则表达式的意思。你能用红色字输入代码来告诉我代码吗?谢谢。我真的是IOException包的新手,我不知道如何申请。如果你能教我的话会很高兴。
Thank you for your help, But i don''t really understand what you mean by using regular expression. Would you show me the code by typing it in red words. Thank you.I really newbie to IOException packages, i don;t really know how to apply. Would be happy if you can teach me.
我现在不担心正则表达式 - 使用异常处理要容易得多。 这里是使用表达式的快速教程。
I wouldn''t worry about regular expressions right now - using Exception handling is a lot easier. Here''s a quick tutorial for using expressions.
这篇关于如何在无法接受String / char的情况下编写验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文