This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading in file  RSS feed

 
shane sare
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

i am trying to write a program that reads in a file that can contain up to 1000 lines. The file contains a bunch of names(string), ages(int), a talent(string),and five scores(doubles). My program has to read the file store the names, ages, and all that stuff into arrays(1D). Reading in the the names were easy but now am trying to put the ages into an array and its not going so well.

My code reads the lines of the file using the readLine() code. I am getting an error saying found string but expected int. Ive tried using ages[i] = Integer.parseInt(fileInput); to make the inut into an integer but it still doesnt work. I might have the code wrong. Here is my try statement:

try
{

file1 = new FileInputStream(fileName);

in = new DataInputStream(file1);



for (int i = 1; i < 1000; i = i++)
{

ages[i] = Integer.parseInt(fileInput);
ages[i] = in.readLine();


System.out.println(ages[i]);



for (int x = 1; x < 8; x++)
{
in.readLine();

} // end for



} // end for



in.close();



} // end try



it is also saying that the code readLine is deprecated. i think that means that there is better code that i could be using. If there is code that will make this easy can someone tell me please.

Thanks in advance
 
stevie liu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" ages[i] = Integer.parseInt(fileInput); "
I don't understand it, what's the 'fileInput'?
 
shane sare
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry here is all of my variables:


import java.io.*;

public class ages
{
public static void main(String[] args)
{


FileOutputStream FileHandle;
FileInputStream file1;
DataInputStream in;
String fileInput = "";
String fileName = "results.txt";
int num = 0;



int[] ages = new int[1000];


that is all the code above my try.
 
stevie liu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shane sare:


String fileInput = "";

- - - - - - -
- - - - - - -

ages[i] = Integer.parseInt(fileInput);





i think two lines above are redundant,
delete them and try it again.
 
shane sare
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i deleted the code and im getting the found string but expected int error. I think i have to convert the lines from the file into an integer of some sort or maybe use sumthing other than readLine().
 
stevie liu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry
i don't understand what you meant
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!