简体   繁体   中英

how do I get data from a text file and save it into a string?

How do I get data from a text file and save it into a string?

For example, my text file has the numbers 1, 4, 5, 6, 8, and 10.4. These numbers can be on the same line or on separate lines. I want to concatenate them into a string, like so: 1 4 5 6 8 10.4

import java.io.File;

import java.io.FileNotFoundException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;

public class f {
    public static void main(String args[]) {
        int count = 0;
        double totalcount = 0;
        double average = 0;
        Scanner read = new Scanner(System.in);
        Scanner file;
        String input = "";
        String test = "";

        double[] array1 = new double[100];

        while (true) {
            System.out.println("Enter name of file or enter quit to exit");
            input = read.next();
            if (input.equalsIgnoreCase("quit")) {
                break;
            }

            try {
                file = new Scanner(new File(input));
                if (!file.hasNextLine()) {
                    System.out.println(input + " file is empty");
                }

                while (file.hasNext()) {
                    totalcount = totalcount + file.nextDouble();
                    count++;
                }
                while (file.hasNext()) {
                    test = test + (" ") + file.next();
                }

                System.out.println("bla" + test);

                average = totalcount / count;

                DecimalFormat df = new DecimalFormat("#.###");
                df.setRoundingMode(RoundingMode.CEILING);

                System.out.println("\nCount: " + count);
                System.out.println("Total: " + df.format(totalcount));
                System.out.println("Average: " + df.format(average));
                System.out.println();

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

My code does not work correctly.

Your code is working fine, the problem is, you trying to access content of your file two times.after first while loop , the hasnext() method will return false.because you already accessed all the element in first while loop. so it will not execute -

                while (file.hasNext()) {
                test = test + (" ") + file.next();
                }

Other than that your code is fine. if you want store it in string also then do small modification in your first while loop as below-

while (file.hasNext()) {
      Double d=file.nextDouble();
      test = test + (" ")+d;
      totalcount = totalcount + d;
      count++;
 }

I think this will give you what you want.

Hello i think below code will be useful for you ,as per your question i have txt file with data , first i am getting the location of file & then i am trying to get the content , at last i am printing it to the console

File f = new File("D:\\temp.txt");

    String content11 = FileUtils.readFileToString(f);

      System.out.println(content11);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM