【发布时间】:2014-03-14 13:44:03
【问题描述】:
这是一个家庭作业,我要读入一个文件并将所有字符转换为大写或小写,具体取决于用户选择的内容。我已经完成了所有这些工作,但我不确定我的进度条是否随着每个字符的写入而更新,或者当文件写入完成时它是否会跳到 100%。我的逻辑是在写入每个字符后,我增加一个计数器,然后在该增量之后我progressBar.setValue(100 * (symbolsWritten / totalSymbols)); 这是我的代码的一部分,用于检查字符是否为小写,并在需要时将其转换为大写。我只是在学习,所以请不要把我的代码打得太糟糕:) 谢谢你的 cmets。
public void writeFileLC(char[] theCharArray)
{
int totalSymbols = theCharArray.length;
int symbolsWritten = 0;
int symbolsConverted = 0;
/*this loop checks to see if the character is upper case and converts
it if needed. Then it writes the characters to the output file via
the output object output*/
for (int i = 0; i < theCharArray.length; i++)
{
if (Character.isUpperCase(theCharArray[i]))
{
try
{
output.format("%c", Character.toLowerCase(theCharArray[i]));
symbolsConverted++;
symbolsWritten++;
Symbols_converted_textfield.setText(String.valueOf(symbolsConverted));
ProgressBar.setValue(100 * (symbolsWritten / totalSymbols));
}//end try block
catch (FormatterClosedException formatterClosedException)
{
JOptionPane.showMessageDialog(this, "Error writing to file",
"Error writing to file", JOptionPane.ERROR_MESSAGE);
}//end catch
}//end if
else
{
try
{
output.format("%c", theCharArray[i]);
symbolsWritten++;
Symbols_converted_textfield.setText(String.valueOf(symbolsConverted));
ProgressBar.setValue(100 * (symbolsWritten / totalSymbols));
}//end try block
catch (FormatterClosedException formatterClosedException)
{
JOptionPane.showMessageDialog(this, "Error writing to file",
"Error writing to file", JOptionPane.ERROR_MESSAGE);
}//end catch
}//end else
}//end for
}//end method writeFileLC
【问题讨论】:
标签: java swing jprogressbar