Try-Catch 块获取字符串长度错误
Try-Catch block getting length of string error
一切正常,但 JTextField 有问题。当用户什么都不提交时,它应该是 returning null
而不是 0
。到目前为止它是 returning 0,但我需要它 return NULL。它根本没有到达 Catch 块,即使我输入 System.out.println("test");
它也没有到达那里。
import javax.swing.*;
import javax.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class stringlength implements ActionListener {
public static JLabel outputLabel;
public static JTextField inputField = new JTextField(20);
public static void main(String[] args) {
stringlength myWindow = new stringlength ();
}
public stringlength (){
JFrame frame = new JFrame("stringlength");
frame.setVisible(true);
frame.setSize(500,100);
//frame.setLayout(new GridLayout(1,3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
outputLabel = new JLabel("String length = ");
JButton stringLengthButton = new JButton("Get String Length");
stringLengthButton.addActionListener(this);
panel.add(stringLengthButton);
panel.add(outputLabel);
panel.add(inputField);
}
public void actionPerformed(ActionEvent e) {
try{
String text = inputField.getText();
System.out.println(text);
outputLabel.setText("String length = " + text.length());
}
catch(NullPointerException e1)
{
e1.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
try{
String text = inputField.getText();
System.out.println(text);
outputLabel.setText("String length = " + text.length());
if (text.length()==0)
throw new NullPointerException();
}
catch(NullPointerException e1)
{
e1.printStackTrace();
}
}
getText()
永远不会 return null
。如果您想要 null
值或异常,则必须检测零长度字符串和 return null
或自己抛出异常。
让我问你...如果你可以 return null。您的代码是否应该让用户再次尝试输入字符串?如果是这样那就试试
try
{
firstTry = inputField.getText();
if(firstTry.length() == 0)
{
outputLabel.setText("String Length = "+secondTry.length());
}
else
{
outputLabel.setText("String length = "+firstTry.length());
}
}//end try
catch(NullPointerException e1)
{
secondTry=JOptionPane.showInputDialog ( "empty try again" );
outputLabel.setText("String Length = "+secondTry.length());
}//end catch
一切正常,但 JTextField 有问题。当用户什么都不提交时,它应该是 returning null
而不是 0
。到目前为止它是 returning 0,但我需要它 return NULL。它根本没有到达 Catch 块,即使我输入 System.out.println("test");
它也没有到达那里。
import javax.swing.*;
import javax.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class stringlength implements ActionListener {
public static JLabel outputLabel;
public static JTextField inputField = new JTextField(20);
public static void main(String[] args) {
stringlength myWindow = new stringlength ();
}
public stringlength (){
JFrame frame = new JFrame("stringlength");
frame.setVisible(true);
frame.setSize(500,100);
//frame.setLayout(new GridLayout(1,3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
outputLabel = new JLabel("String length = ");
JButton stringLengthButton = new JButton("Get String Length");
stringLengthButton.addActionListener(this);
panel.add(stringLengthButton);
panel.add(outputLabel);
panel.add(inputField);
}
public void actionPerformed(ActionEvent e) {
try{
String text = inputField.getText();
System.out.println(text);
outputLabel.setText("String length = " + text.length());
}
catch(NullPointerException e1)
{
e1.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
try{
String text = inputField.getText();
System.out.println(text);
outputLabel.setText("String length = " + text.length());
if (text.length()==0)
throw new NullPointerException();
}
catch(NullPointerException e1)
{
e1.printStackTrace();
}
}
getText()
永远不会 return null
。如果您想要 null
值或异常,则必须检测零长度字符串和 return null
或自己抛出异常。
让我问你...如果你可以 return null。您的代码是否应该让用户再次尝试输入字符串?如果是这样那就试试
try
{
firstTry = inputField.getText();
if(firstTry.length() == 0)
{
outputLabel.setText("String Length = "+secondTry.length());
}
else
{
outputLabel.setText("String length = "+firstTry.length());
}
}//end try
catch(NullPointerException e1)
{
secondTry=JOptionPane.showInputDialog ( "empty try again" );
outputLabel.setText("String Length = "+secondTry.length());
}//end catch