Question

I have the code. Some of names are in Polish, but I think it's not a problem. In this code I create randomly filled array, write it to the .txt file and then create another array using data read from this file. Unfortunately in static method hasNextDouble() method return false and my array is filled with 0.0's. Why is that so? If I open this .txt file via notepad it contains doubles that can be read. Where is a problem?

package algorytmyz1;
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Random; 
import java.util.Scanner; 

class Tablica {
private int w;
private int k;
private double[][] tab;
//konstruktor
public Tablica(int w,int k) {
    this.w = w;
    this.k = k;
    tab = new double[w][k];
    Wypelnij();
    Zapisz();
}

//wypelnia macierz liczbami od 0 do 9
private void Wypelnij(){

    Random rand = new Random();

    for(int i=0; i<w; i++)
        for(int j=0; j<k; j++)
                 //generuje pseudolosowe liczby rzeczywiste między 0 a 10
            tab[i][j]=rand.nextDouble() * 10;
}

private void Zapisz() {
    try (PrintWriter fout = new PrintWriter("macierz1.txt"))
    {
        fout.println("Macierz:");
        fout.println(w);
        fout.println(k);
        for (int i=0; i<w; i++)
        {
            for (int j=0; j<k; j++)
            {
                fout.print(tab[i][j] + "  ");
            }
            fout.println();
        }
    }
    catch (IOException e) {
        System.out.println("Błąd wejścia/wyjścia.");
    }
}

}

public class AlgorytmyZ1 {

public static void main(String[] args) throws FileNotFoundException {
    int w,k;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    //wprowadzenie liczby wierszy i kolumn macierzy
    try {
    System.out.println("Podaj liczbę wierszy:");
    w = Integer.parseInt(br.readLine());
    System.out.println("Podaj liczbę kolumn:");
    k = Integer.parseInt(br.readLine());

    Tablica macierz = new Tablica(w,k);
    }
    catch (IOException e) {
        System.out.println(e);
    }

    WczytajWyswietl();
}

public static void WczytajWyswietl() throws FileNotFoundException {
    int w, k;
    String naglowek;
    double[][] tablica;

    File file = new File("macierz1.txt");
    Scanner fin = new Scanner(file);

    naglowek = fin.nextLine();
    w = Integer.parseInt(fin.nextLine());
    k = Integer.parseInt(fin.nextLine());

    tablica = new double[w][k];
    while (fin.hasNextDouble())
    {
        for (int i=0; i<w; i++)
        {
            for (int j=0; j<k; j++)
                tablica[i][j] = fin.nextDouble();
        }
    }

    System.out.println(naglowek);
    System.out.println("Liczba wierszy: " + w);
    System.out.println("Liczba kolumn: " + k);
    for (int i = 0; i<w; i++)
    {
        for (int j=0; j<k; j++)
        {
            System.out.print(tablica[i][j] + " ");
        }
          System.out.println();
    }

}

}

No correct solution

OTHER TIPS

The correct answer was that I had to use useLocale(Locale.English) method on my scanner object. I don't know which Locale was set before, but with english everything runs smooth. I post this answer to my own post because maybe it will be helpful for someone in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top