Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey guys, Using Java, im reading in an input file using scanner and i am able to

ID: 3668863 • Letter: H

Question

Hey guys, Using Java, im reading in an input file using scanner and i am able to read and print out the input file. But i am having difficulty parsing it now.

The example input file is as follows :

height 6

weight 120

name john

team played for team1 start 2010 end 2012

team played for team2 start 2013 end 2015

team played for team3 start 2002 end 2008

how do i go about parsing this information. currently im scanning the input by line like:

while (scanner.hasNext()) {

String line = scanner.nextLine();

System.out.println(line);

}

Explanation / Answer

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;  

import java.io.*;  

  

   

class FileReader  

{    

public static void main(String[] args) throws IOException

{

File file = new File("data.txt");

List<OpenGLCoordinates> coords = new ArrayList<OpenGLCoordinates>();

  

try {

Scanner scanner = new Scanner(file);

while (scanner.hasNextDouble()) {

double x = scanner.nextDouble();

double y = scanner.nextDouble();

double z = scanner.nextDouble();

OpenGLCoordinates openGLCoords = new OpenGLCoordinates(x,y,z);

coords.add(openGLCoords);

}

System.out.println();

}

catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

here am doing as example one