Мне нужно создать программу, которая читает файл и отслеживает, сколько из них доступно для печати. У меня все все, кроме как в конце мне нужно отобразить общее количество печатных символов. Я не знаю, как добавить все значения массива вместе, чтобы получить, что:Поиск суммы массива Java
public static void main(String[] args) throws IOException {
final int NUMCHARS = 95;
int[] chars = new int[NUMCHARS];
char current; // the current character being processed
// set up file output
FileWriter fw = new FileWriter("output.txt"); // sets up a file for output
BufferedWriter bw = new BufferedWriter(fw); // makes the output more efficient. We use the FileWriter (fw) created in previous line
PrintWriter outFile = new PrintWriter(bw); // adds useful methods such as print and println
// set up file for input using JFileChooser dialog box
JFileChooser chooser = new JFileChooser(); // creates dialog box
int status = chooser.showOpenDialog(null); // opens it up, and stores the return value in status when it closes
Scanner scan;
if(status == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
scan = new Scanner (file);
}
else
{
JOptionPane.showMessageDialog(null, "No File Choosen", "Program will Halt", JOptionPane.ERROR_MESSAGE);
outFile.close();
return;
}
while(scan.hasNextLine())
{
String line = scan.nextLine();
// Count the number of each letter occurrence
for (int ch = 0; ch < line.length(); ch++)
{
current = line.charAt(ch);
chars[current-' ']++;
}
// Print the results
for (int letter=0; letter < chars.length; letter++)
{
if(chars[letter] >= 1)
{
outFile.print((char) (letter + ' '));
outFile.print(": " + chars[letter]);
outFile.println();
}
}
}
outFile.close();
scan.close(); }
Вы ищете сумму всех значения в массиве или сколько значений в массиве есть? – Dando18