Home > OS >  reading and writing files java
reading and writing files java

Time:01-23

I have a float array which I stored in it some values from user input. I have 2 methods one that saves the values stored in the array to a text file each value on a line and the second method rereads the values again and stores them in the array. for example, the user input was 1,2,3,4 I save them to a text file and then I read the same txt file now my array should display 8 elements 1,2,3,4,1,2,3,4. the problem I'm having is that when I store these elements on the txt file it's storing them and adding like 100 zeros under them and when I'm calling the second method to reread these elements from the file it reads the zeros so when I'm displaying the elements in my array it's displaying 0,0,0,0 when it should display 1,2,3,4,1,2,3,4. what might be causing me this problem?

public void saveValuesToFile(Scanner keyboard) {
    try {
        System.out.println("Enter name of file: ");
        String fileName = keyboard.next();
        File file = new File(fileName);
        PrintWriter outputFile = new PrintWriter(file);

        for(int i = 0; i < numbers.length; i  ) {
            outputFile.println(numbers[i]);
        }

        outputFile.close();



    } catch (FileNotFoundException e) {
        System.out.println("file dont exist");
        e.printStackTrace();
    }

}


public void readFromFile(Scanner keyboard) {
    
    System.out.println("Enter file name");
    String fileName = keyboard.next();
    BufferedReader reader = null;
    try {
        reader  = new BufferedReader (new FileReader(fileName));
        String input = null;

        while ((input = reader.readLine()) != null) {
            for (int i = 0; i < numbers.length; i  ) {
                numbers[i] = Float.parseFloat(input);
    }
    }
}
    catch (NumberFormatException | IOException e) {
        e.printStackTrace();
 }
}

CodePudding user response:

You may check why the array is populated properly using additional println statement. In your version each element of array is populated with the same element read from the file. If you remove the inner loop, array will be populated properly.

   int i=0;
    while ((input = reader.readLine()) != null) {
        
         numbers[i] = Float.parseFloat(input);
         System.out.println((i)   "::" numbers[i]);
         i  ;
     }

CodePudding user response:

Zeros are being added because you're saving numbers as float. If you store an integer 3 in a float variable it will be converted to a float equivalent which is 3.0

Also you don't need two loops here,

while ((input = reader.readLine()) != null) {
            for (int i = 0; i < numbers.length; i  ) {
                numbers[i] = Float.parseFloat(input);
    }

You can instead do following,

int i = 0;
while ((input = reader.readLine()) != null) {
   numbers[i] = Float.parseFloat(input);
   i  ;
  }

Following is a fully functional program of what you desire,

import java.util.Scanner;
import java.io.*;

public class Hello {
    
    public static float[] numbers = {1,2,3,4,1,2,3,4};
    
    public static void saveValuesToFile(Scanner keyboard) {
        try {
            System.out.println("Enter name of file: ");
            String fileName = keyboard.next();
            File file = new File(fileName);
            PrintWriter outputFile = new PrintWriter(file);

            for(int i = 0; i < numbers.length; i  ) {
                outputFile.println(numbers[i]);
            }

            outputFile.close();



        } catch (FileNotFoundException e) {
            System.out.println("file doesn't exist");
            e.printStackTrace();
        }

    }


    public static void readFromFile(Scanner keyboard) {
        
        System.out.println("Enter file name");
        String fileName = keyboard.next();
        BufferedReader reader = null;
        try {
            reader  = new BufferedReader (new FileReader(fileName));
            String input = null;
            int i = 0;
            while ((input = reader.readLine()) != null) {
                    numbers[i] = Float.parseFloat(input);
                    i  ;
            }
            
            for(int j = 0; j < numbers.length; j  ) {
                System.out.println(numbers[j]);
            }
        }
        catch (NumberFormatException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       saveValuesToFile(scanner);
       readFromFile(scanner);
    }

}
    
  •  Tags:  
  • Related