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.

Recommended Answers

All 8 Replies

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.

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

For sorting the arrays you can choose one of several sorting algorithms. Simple ones will be bubble sort, selection sort and insertion sort. Try implementing one of these. The links above contain all the information you need regarding those sorts :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.