[英]Java, Output not being displayed?
The task was to display the users full name, a random even number and initials. 任务是显示用户的全名,随机偶数和首字母缩写。
For example. 例如。 Joshua Ethan Paul Smith 约书亚·伊森·保罗·史密斯
SmithJEPS320 史密斯JEPS320
When I press my get code button nothing happens, why is this? 当我按下获取代码按钮时,什么也没发生,这是为什么?
package pack;
import javax.swing.JOptionPane;
import java.math.*;
public class saf extends javax.swing.JFrame {
public saf() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnfullname = new javax.swing.JButton();
txaDisplay = new javax.swing.JScrollPane();
TxaDisplay = new javax.swing.JTextArea();
btnrandomGenerator = new javax.swing.JButton();
btnCode = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnfullname.setText("Enter");
btnfullname.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnfullnameActionPerformed(evt);
}
});
TxaDisplay.setEditable(false);
TxaDisplay.setColumns(20);
TxaDisplay.setRows(5);
txaDisplay.setViewportView(TxaDisplay);
btnrandomGenerator.setText("Generate Random Number");
btnrandomGenerator.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnrandomGeneratorActionPerformed(evt);
}
});
btnCode.setText("Get Code!");
btnCode.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCodeActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(54, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(txaDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btnrandomGenerator)
.addGap(112, 112, 112))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btnCode)
.addGap(165, 165, 165))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btnfullname)
.addGap(177, 177, 177))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(btnfullname)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txaDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnrandomGenerator)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnCode)
.addContainerGap(28, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnfullnameActionPerformed(java.awt.event.ActionEvent evt) {
String fullname = JOptionPane.showInputDialog("Enter your full name:");
TxaDisplay.append(fullname);
}
private void btnrandomGeneratorActionPerformed(java.awt.event.ActionEvent evt) {
int randnum; //Code for a 3 digit even number
do
{
randnum=(int)(Math.random()*900)+100;
}
while(randnum%2!=0);
TxaDisplay.append("" + randnum);
}
private void btnCodeActionPerformed(java.awt.event.ActionEvent evt) {
int randnum = 0 ;
String fullname = "" ;
String initials ="";
int posSpc = fullname.lastIndexOf(" ");
String surname = fullname.substring(posSpc+1, fullname.length());
initials = initials + fullname.charAt(0);
for(int i =0; i< fullname.length() ; i++)
{
char ch=fullname.charAt(i);
if(ch==' ')
{
initials = initials + fullname.charAt(i+1);
}
}
String code = surname + initials + "" + randnum;
TxaDisplay.append(fullname+"\n");
TxaDisplay.append(code);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(saf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(saf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(saf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(saf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new saf().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextArea TxaDisplay;
private javax.swing.JButton btnCode;
private javax.swing.JButton btnfullname;
private javax.swing.JButton btnrandomGenerator;
private javax.swing.JScrollPane txaDisplay;
// End of variables declaration
}
It's rather easy. 这很容易。 You initiate fullname to the empty string and then try to access the first character of the empty String
--> StringIndexOutOfBoundException
: 您发起全称为空字符串,然后尝试访问空的第一个字符String
- > StringIndexOutOfBoundException
:
int randnum = 0 ;
String fullname = "" ;// Here empty string !!!
String initials ="";
int posSpc = fullname.lastIndexOf(" "); // this is always -1
String surname = fullname.substring(posSpc+1, fullname.length()); // This is the empty string
initials = initials + fullname.charAt(0); // That throws a StringIndexOutOfBoundsException
If you want this to work, you need to: 如果您希望这样做,则需要:
private String fullname
制作一个私有变量: private String fullname
fullname
and access the private variable of your class instead 摆脱所有局部变量fullname
,改为访问类的私有变量 btnCodeActionPerformed
, check that fullname
is not empty 在btnCodeActionPerformed
,检查fullname
不为空 Something like this (not tested): 像这样的东西(未经测试):
public class saf extends javax.swing.JFrame {
private String fullname;
public saf() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnfullname = new javax.swing.JButton();
txaDisplay = new javax.swing.JScrollPane();
TxaDisplay = new javax.swing.JTextArea();
btnrandomGenerator = new javax.swing.JButton();
btnCode = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnfullname.setText("Enter");
btnfullname.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnfullnameActionPerformed(evt);
}
});
TxaDisplay.setEditable(false);
TxaDisplay.setColumns(20);
TxaDisplay.setRows(5);
txaDisplay.setViewportView(TxaDisplay);
btnrandomGenerator.setText("Generate Random Number");
btnrandomGenerator.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnrandomGeneratorActionPerformed(evt);
}
});
btnCode.setText("Get Code!");
btnCode.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCodeActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(54, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(txaDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btnrandomGenerator)
.addGap(112, 112, 112))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btnCode)
.addGap(165, 165, 165))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btnfullname)
.addGap(177, 177, 177))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(btnfullname)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txaDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnrandomGenerator)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnCode)
.addContainerGap(28, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnfullnameActionPerformed(java.awt.event.ActionEvent evt) {
fullname = JOptionPane.showInputDialog("Enter your full name:");
TxaDisplay.append(fullname);
}
private void btnrandomGeneratorActionPerformed(java.awt.event.ActionEvent evt) {
int randnum; //Code for a 3 digit even number
do
{
randnum=(int)(Math.random()*900)+100;
}
while(randnum%2!=0);
TxaDisplay.append("" + randnum);
}
private void btnCodeActionPerformed(java.awt.event.ActionEvent evt) {
if(fullname==null||fullname.trim().isEmpty()) {
return;
}
int randnum = 0 ;
String initials =;
int posSpc = fullname.lastIndexOf(" ");
String surname = fullname.substring(posSpc+1, fullname.length());
initials = fullname.charAt(0);
for(int i =0; i< fullname.length() ; i++)
{
char ch=fullname.charAt(i);
if(ch==' ')
{
initials = initials + fullname.charAt(i+1);
}
}
String code = surname + initials + "" + randnum;
TxaDisplay.append(fullname+"\n");
TxaDisplay.append(code);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(saf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(saf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(saf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(saf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new saf().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextArea TxaDisplay;
private javax.swing.JButton btnCode;
private javax.swing.JButton btnfullname;
private javax.swing.JButton btnrandomGenerator;
private javax.swing.JScrollPane txaDisplay;
// End of variables declaration
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.