0

i can't seem to find a way so the file does not overwrite each time i continue my loop. Can't seem to find away to save old value of array so it does not overwrites.

Writes user input into array:

Scanner menu = new Scanner(System.in);
 int sakums = menu.nextInt();
 switch(sakums)
 {
 case 1:
Scanner in = new Scanner(System.in);

//get the input for number of students:
System.out.println("Studentu skaits:");
int totalstudents = in.nextInt();
in.nextLine();// just to ignore the line
//store into String array

String studentname[] = new String[totalstudents];

for(int i = 0; i < studentname.length;i++)
{

    System.out.println("Ievadi Uzvardu Vardu : "+i);
    studentname[i] = in.nextLine();
}
System.out.println("-----------------Ievade------------------------");
for(String names:studentname)
{

    System.out.println(names);
}

Writes array into file:

FileWriter fw = new FileWriter("C:\\input.txt");
for (int i = 0; i < studentname.length; i++) {
  fw.write(studentname[i] + "\n");
  int jaunais = i;
}
fw.close();

      break;
      case 2:
Kara
  • 23
  • 6
  • Fix your formatting :) – Tyler Nichols Sep 13 '17 at 12:39
  • 2
    `FileWriter fw = new FileWriter("C:\\input.txt");` resets the existing file. You might want to use append mode. – Eran Sep 13 '17 at 12:40
  • Also it looks like you ware facing this problem: [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](https://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods). Instead of creating another Scanner simply consume line separator left after `nextInt` call. – Pshemo Sep 13 '17 at 12:53

0 Answers0