Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:05:42+00:00 2026-06-06T19:05:42+00:00

I am doing this homework where I am supposed to read a .txt file

  • 0

I am doing this homework where I am supposed to read a .txt file filled with integers into a double array, and then write this double array into another .txt file.

At first I thought everything was working as I was able to read the file and display it out as an image, and then save it into a .txt file as doubles. However, when I tried to use the same class file for another lesson (display it in frame I created), I kept getting 0.0 as the value of the output .txt file. Below is the code for my read and write class:

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class IO {
  FileDialog fd;
  public double[][] readData() {
    fd = new FileDialog(new Frame(), "Open Files", FileDialog.LOAD);
    fd.setVisible(true);
    File f = null;
    if ((fd.getDirectory() != null)||( fd.getFile() != null)) {
      f = new File(fd.getDirectory() + fd.getFile());
    }
    FileReader fr = null;
    try {
      fr = new FileReader (f);
    } catch (FileNotFoundException fnfe) {
      fnfe.printStackTrace();
    }
    BufferedReader br = new BufferedReader(fr);
    int lines = -1;
    String textIn = " ";
    String[] file = null;
    try {
      while (textIn != null) {
        textIn = br.readLine();
        lines++;
      }
      file = new String[lines];
      fr = new FileReader (f);
      br = new BufferedReader(fr);
      for (int i = 0; i < lines; i++) {
        file[i] = br.readLine();
      }
      br.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
    double[][] data = new double [lines][];
    for (int i = 0; i < lines; i++) {
      StringTokenizer st = new StringTokenizer(file[i],",");
      data[i] = new double[st.countTokens()];
      int j = 0;
      while (st.hasMoreTokens()) {
        data[i][j] = Double.parseDouble(st.nextToken());
        j++;
      }
    }
    return data;
  }


  public void writeData(double[][] dataIn) {
    fd = new FileDialog(new Frame(), "Save Files", FileDialog.SAVE);
    fd.setVisible(true);
    File f = null;
    if ((fd.getDirectory() != null)||( fd.getFile() != null)) {
      f = new File(fd.getDirectory() + fd.getFile());
    }
    FileWriter fw = null;
    try {
      fw = new FileWriter (f, true);
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
    BufferedWriter bw = new BufferedWriter (fw);
    String tempStr = "";
    try {
      for (int i = 0; i < dataIn.length; i++) {
        for (int j = 0; j < dataIn[i].length; j++) {
          tempStr = String.valueOf(dataIn[i][j]);
          bw.write(tempStr);
        }
        bw.newLine();
      }
      bw.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}

The txt file I am trying to read have 300 rows and column, of which not all column are filled to full with int number. I could get the input txt to be Display but could not save it back as a txt file with the same values but in double instead of int.

Could someone help me out here please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:05:43+00:00Added an answer on June 6, 2026 at 7:05 pm

    I’m not sure I understand your complete problem, but you have at least the problem that you forget to add a comma after each number when you write to the file. Since readData uses commas to separate numbers, files produced by your program cannot be read by it. Simply change

    for (int j = 0; j < dataIn[i].length; j++) {
              tempStr = String.valueOf(dataIn[i][j]);
              bw.write(tempStr);
    

    to

    for (int j = 0; j < dataIn[i].length; j++) {
              bw.write(String.valueOf(dataIn[i][j]) + ",");
    

    (removed the intermediate tempStr since it is unnecessary as your code is now).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

well I'm doing this homework but I'm still not viewing the fault... When I
How should I proceed on doing this? This IS a homework, and I'm having
I'm doing a homework project that requires this: Below you will find the code
This is NOT a homework question, actually I am doing this for fun. Here
this is a homework question. I am doing diffs on our domain model and
This is a homework problem that I have. I have been doing some research
I want to list all integers that divide n. This is a homework question.
This is for a homework assignment. I am supposed to change the contents of
I am doing this homework task where it requires me to take in a
I am new to python and doing this homework. I need to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.