Pls I\'m not able to read the datafile as it been lay down due to the empty spac
ID: 3620145 • Letter: P
Question
Pls I'm not able to read the datafile as it been lay down due to the empty spaces on column 1 and 2. Can some one help?Below are the data file input
-999999 0 -999999 0
1 2 0 1
1 3 1 6
1 4 2 8
1 5 3 9
2 7 4 10
5 12 4 11
6 14 5 13
8 17 6 15
8 18 7 16
9 22 8 19
999999 0 8 20
8 21
9 23
9 24
9 25
999999 0
import java.io.*;
import java.util.*;
public class Proj1Main
{
private static Scanner ins = new Scanner(System.in);
public static void main(String[] args)
{
String filename;
int maxNum = 20;
int[] intArray = new int[maxNum];
int[] bIntArray = new int[maxNum];
int[] aRanks = new int[maxNum];
int[] bRanks = new int[maxNum];
System.out.println("Enter the name of the input file: ");
filename = ins.next();
Scanner input = null;
try
{
input = new Scanner(new File(filename));
}
catch(FileNotFoundException FNFE)
{
System.err.printf("Could not find the input file %s ", filename);
System.exit(1);
}
String comment = input.nextLine(); // to go to next line in input
int index = 0;
String emptyLine;
while ((index <= maxNum) && input.hasNext())
{
int iArrayElemnts;
int aRankElemnts;
int jArrayElemnts;
int bRankElemnts;
iArrayElemnts = input.nextInt(); //read all first sequence inputs
aRankElemnts = input.nextInt(); //read all second sequence inputs
jArrayElemnts = input.nextInt(); //read all third sequence inputs
bRankElemnts = input.nextInt(); //read all fourth sequence inputs
intArray[index] = iArrayElemnts; //assign all first sequence inputs into intArray array
aRanks[index] = aRankElemnts; //assign all second sequence inputs into aRanks array
bIntArray[index] = jArrayElemnts; //assign all third sequence inputs into bIntArray array
bRanks[index] = bRankElemnts; //assign all fourth sequence inputs into bRanks array
index += 1;
}
for(int k=0;k<intArray.length;k++)
System.out.println(intArray[k]);
for(int k=0;k<bIntArray.length;k++)
System.out.println(bIntArray[k]);
//ranksByMerge();
}
}
Explanation / Answer
please rate - thanks have to read it as a string and convert it to integersimport java.io.*;
import java.util.*;
public class Proj1Main
{
private static Scanner ins = new Scanner(System.in);
public static void main(String[] args)
{
String filename;
int maxNum = 20;
int[] intArray = new int[maxNum];
int[] bIntArray = new int[maxNum];
int[] aRanks = new int[maxNum];
int[] bRanks = new int[maxNum];
System.out.println("Enter the name of the input file: ");
filename = ins.next();
Scanner input = null;
try
{
input = new Scanner(new File(filename));
}
catch(FileNotFoundException FNFE)
{
System.err.printf("Could not find the input file %s ", filename);
System.exit(1);
}
String comment = input.nextLine(); // to go to next line in input
int index = 0,lena=0,lenb=0;
String emptyLine;
String inputstring;
while ((lena<=maxNum&&lenb <= maxNum) && input.hasNextLine())
{
inputstring=input.nextLine();
String[] inputs=inputstring.split(" ");
intArray[lena] = Integer.parseInt(inputs[0]); //assign all first sequence inputs into intArray array
aRanks[lena] = Integer.parseInt(inputs[1]); //assign all second sequence inputs into aRanks array
lena++;
if(inputs.length>2)
{
bIntArray[lenb] = Integer.parseInt(inputs[2]); //assign all third sequence inputs into bIntArray array
bRanks[lenb] = Integer.parseInt(inputs[3]); //assign all fourth sequence inputs into bRanks array
lenb++;
}
}
for(int k=0;k<lena;k++)
System.out.println(intArray[k]);
for(int k=0;k<lenb;k++)
System.out.println(bIntArray[k]);
//ranksByMerge();
}
}