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 6791793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:53:21+00:00 2026-05-26T17:53:21+00:00

I have a textfile with some values. textfile data line1row1 line1row2 55 line2row1 line2row2

  • 0

I have a textfile with some values.

textfile data

line1row1 line1row2 55
line2row1 line2row2 44
line3row1 line3row2 33

I have a data class where i have created a contructor. the data which goes into the array, i want read from the text file.

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

class Data{
    public Data(String entry1, String entry2, int entry3){}
}   
public class readData {
    public static void main(String[] args) throws Exception{
    BufferedReader inFile = new BufferedReader (new FileReader ("scores.txt"));

    Data entrydata[] = new Data[3]; //create new constructor array
    for(int i = 0; i < entrydata.length; i++ ){
            entrydata[i] = inFile.readLine();
        }
    }
}

I get an error on “inFile.readLine()”… Cannot Convert from String to Data(where “Data” refers to class)

I can hardcode the data [as below] but want it to be read from the file instead

Data entrydata[] = new Data[3];
entrydata [0] = new Data("line1row1 ", "line1row2 ", 55);
entrydata [1] = new Data("line2row1 ", "line2row2 ", 44);
entrydata [2] = new Data("line3row1 ", "line3row2 ", 33);

The reason I want to do this, is so that I can access the informatin stored in the array.

  • 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-05-26T17:53:22+00:00Added an answer on May 26, 2026 at 5:53 pm

    inFile.readline() returns a string, which isn’t a Data object and so you get the error message. You need to split the string you read from the file into an array and the use the array elements to create your data items. Something like:

    String[] tmp = inFile.readline().split( " " );
    entrydata[i] = new Data( tmp[0], tmp[1], Integer.parseInt( tmp[2] ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text file(test.data), which include some values and class name, for example 4.5,3.5,U1
I have an XML file which contains some data as given. <?xml version=1.0 encoding=UTF-8
I have a text file which was created using some Microsoft reporting tool. The
I have been given a textfile which contains data on time sheet. That line
We have some C# code that reads data from a text file using a
I have to parse a tab-delimited text file with Ruby to extract some data
I have a large textfile, which has linebreaks at column 80 due to console
I have a text file with a large amount of data which is tab
I have some class with lots of fields; public class CrowdedHouse { public int
I have a NetCDF file, which contains data representing total precipitation across the globe

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.