i want to read file data as below. [ int, double, double]
1234 12.30 45.60
2345 23.40 67.50
3456 45.60 67.80
.

into multiple arrays of type int, double and double.
any help will be appreciated.

The file - is it binary or text? You want the first number to go into the int array, the next to one double array and the third to the second double array, and so on for each line?

input file is text file. please see code below and let me know if i am on right track. if not please share your input.

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

public class Assgn01{
	public static void main(String[] args) throws IOException{
		//Variables
		final int ROWS = 20;
		int Counter = 0;

		File f = new File("data.txt");
		if (!(f.exists())){
			System.out.println("Closing the program");
			System.exit(0);
		}

		//create a scanner class for reading the file
		Scanner InputFile = new Scanner(f);

		//create the arrays for holding the data from the file
		int[] id = new int[ROWS];
		double[] gra = new double[ROWS];
		double[] grb = new double[ROWS];
		double[] lowest = new double[ROWS];

		//use a while loop with hasNext to fill all the arrays with the data
		while(InputFile.hasNext()){
			id[Counter] = InputFile.nextInt();
			gra[Counter] = InputFile.nextDouble();
			grb[Counter] = InputFile.nextDouble();
			Counter++;
		}

		//transfer the least value to the leastSccr Method to calc least value
		leastScr(lowest);
	}

	public static void leastScr(double[] Array){
		double[] least = new double[1];

		for(int Index = 0; Index < Array.length; Index++){
			if(Array[Index] < least[0]){
				least[0] = Array[Index];
			}
		}
	}
}

What is the problem? The program works. If you are worried about the extra zeros - it is because your array is of size 20, and you are inputting only 3 numbers to it.

I want to display the arrays in sorted and unsorted way, if you could help me with methods?

The easiest way will be using the Arrays class, it has many nice features, among them sorting an array.

Edited 6 Years Ago by apines: n/a

I can't seem to have display working. can you please help? on either sorted or unsorted info? really appreciate

This article has been dead for over six months. Start a new discussion instead.