문자열을 입력하고 사용 된 각 문자의 수를 출력하는 숙제가 있습니다. 내가 JOptionPane을 사용할 때 논리가 작동하고 있었고 숙제가 이미 돌아 왔습니다. 그러나 JFrame JTextArea 입출력을 사용하도록 코드를 '변환'하려고하면 출력에 올바른 문자 수가 표시되지 않습니다. 나는 이것이 나의 논리에 매우 간단한 무언가를 확신 해요,하지만 난 그것을 여기에루프 논리 오류에 대한 A
같이 출력이 모습입니다 표시되지 않습니다 :
여기
코드
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Launcher extends JFrame
{
// create panel components
private static JTextArea txaUserInput;
private static JTextArea txaResults;
private static JButton btnSearch;
public static void main(String[] args)
{
JFrame frame = new JFrame("Final Answer");
Border b = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GRAY);
txaUserInput = new JTextArea();
txaUserInput.setPreferredSize(new Dimension(220, 70));
txaUserInput.setBorder(b);
txaUserInput.setLineWrap(true);
txaResults = new JTextArea("Results");
txaResults.setPreferredSize(new Dimension(200, 500));
txaResults.setBorder(b);
btnSearch = new JButton("Count Occurences of Each Letter");
frame.setLayout(new FlowLayout());
frame.add(txaUserInput);
frame.add(btnSearch);
frame.add(txaResults);
createAndShowGUI();
frame.setSize(300, 700);
frame.setVisible(true);
}
private static void createAndShowGUI()
{
btnSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// create variables
int[] counts = new int[26];
int count = 0;
String input = "";
String output ="";
for (short i = 0; i < (input).length(); i++)
{
char temp = Character.toLowerCase((input).charAt(i));
if (temp >= 'a' && temp <= 'z')
counts[temp - 'a']++;
}
for (short i = 0; i < counts.length; i++)
{
output += (char) ('a' + i) + ":\t " + counts[i] + "\n";
}
for (short i = 0; i < (input).length(); i++)
{
if ((input).charAt(i) == 'a' || (input).charAt(i) == 'A')
count++;
}
txaResults.setText(output);
}
});
}
}
입니다
코드 예제를 발견 한 부분 만 줄일 수 있습니까? –
'input'과'output'을 빈 문자열에 할당하는 이유는 무엇입니까? – SudoRahul
'input' 변수에 대괄호를 쓸 필요가 없습니다. –