How to make a task, just to make it easier, that is, to bind words from a file to an array differently?

import java.io.*; public class Dz51_4 { // создаёт переменные и присваивает им соотв. значения. static String Str, FReadPath = "C:\\Имена.txt", FWritePath = "C:\\Dz51-4.rez" ; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // читает имена из файла Имена.txt, запоняет массив MassName этими именами, сортирует массив по убыванию // и записывает результат в файл Dz51-4.rez. try { BufferedReader FBufR = new BufferedReader (new FileReader (FReadPath)); BufferedWriter FBufWr = new BufferedWriter (new FileWriter (FWritePath)); // создаёт массив Mass с именами из файла Имена.txt. String Mass []; // = {"Саша", "Маша", "Юля", "Даша", "Глаша", "Андрей", "Вася", "Коля", "Петя", "Костя"}; // создаёт массив имён MassName, который надо заполнить именами. String MassName []; // = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "}; while ((Str = FBufR.readLine()) != null){ // читает строки. MassName = Mass; // заполняет массив MassName. } // while // сортирует массив имён по убыванию. for (int curr = 0; curr < Mass.length - 1; curr++){ // перебираем текущие ячейки. for (int i = curr +1; i < Mass.length; i ++){ // перебор нижестоящей ячейки. if (MassName [curr].compareTo(MassName[i]) < 0){ // проверка равенства ячеек. // вытеснение самой большой ячейки в ячейку Mass [curr]. String Buff; Buff = MassName [curr]; MassName [curr] = MassName [i]; MassName [i] = Buff; } // if } // for i } // for curr for (int i = 0; i < Mass.length; i ++){ FBufWr.write(String.valueOf(MassName [i]) + ","); // заносит i-ую ячейку массива в файл. } // for i FBufWr.close(); // сохраняет все изменения в файле и записывает его на диск. } // try catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // catch catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // catch } } 

The names are given in the string String Mass.

  • Thank you in advance. - Tolik
  • I, personally, did not understand anything. After editing the question, please formulate - What is the matter. Not by a piece of code, but by the Russian language - what the task is and what the given code does not suit. - drdaeman 4:19
  • The teacher requires that the binding of words from a file be made differently not so String Mass []; // = {"Sasha", "Masha", "Yulia", "Dasha", "Glasha", "Andrey", "Vasya", "Kolya", "Petya", "Kostya"}; // creates an array of names MassName, which must be filled with names. String MassName []; // = {"", "", "", "", "", "", "", "", "", ""}; but somehow more universal. That is, to bind somehow an array to a file with names. But more simplistic. - Tolik
  • And what is the essence of the problem in general? - zenith

1 answer 1

If the prepad, for some reason does not like the array, where the keys are numbers, then you can create a hash (aka associative array).
See class Hashtable
In theory, then it will be easier to manage the data.