package DS_project_s17; import java.io.File; import java.util.Scanner; import ja
ID: 3842410 • Letter: P
Question
package DS_project_s17;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class RecSys {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Scanner input = new Scanner(System.in); //Scanner object for keyboard input
System.out.print( "Enter the filename: " ); // Prompt the user for a file name
String fileName = input.nextLine(); // get a file name from the user
//If the full path is not given, then the file must be on the same folder as the source java file. //Otherwise, the user must give the full path
File inputFile = new File(fileName ); // create a File object
//Another Scanner object for file input
Scanner fileInput = new Scanner(inputFile);
//read line by line
fileInput.nextLine(); //skip the first line in the text file
while (fileInput.hasNextLine()) //Read from file as long as you have new line
{
String currentline = fileInput.nextLine(); //this holds the current line
/***
* trim(): removes the white space before and after a string
* split(): split the line into terms
* [\s\xA0]: split the line based on white space and uniCode non-breaking terms
* + is to remove more than one white space if exist
*/
//this holds the terms of the line into an array of strings temporarily
String[] terms = currentline.trim().split("[\s\xA0]+");
for (int i = 0; i < terms.length; i++) //iterate each term in a line
{
// i == 0 is the Trans #, you can skip it
if(i == 1){ // process the user name
System.out.println(terms[i]); //output for testing only (remove it later)
/**
* Here process and store the Username into your desired data structure
*/
}
else if(i > 1){ // process the itemIDs
int itemID = Integer.parseInt(terms[i]);
System.out.println(itemID); //output for testing only (remove it later)
/**
* Here process and store the itemIDs into your desired data structure
*/
}
}//end for
}//end while
input.close();
fileInput.close();
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}//end main
}
Explanation / Answer
please find the below code:
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class RecSys {
public static void main(String[] args) {
try {
Scanner input = new Scanner(System.in); //Scanner object for keyboard input
System.out.print( "Enter the filename: " ); // Prompt the user for a file name
String fileName = input.nextLine(); // get a file name from the user
File inputFile = new File(fileName ); // create a File object
Scanner fileInput = new Scanner(inputFile);
fileInput.nextLine(); //skip the first line in the text file
while (fileInput.hasNextLine()) //Read from file as long as you have new line
{
String currentline = fileInput.nextLine(); //this holds the current line
String[] terms = currentline.trim().split("[\s\xA0]+");
for (int i = 0; i < terms.length; i++) //iterate each term in a line
{
if(i == 1){ // process the user name
System.out.println(terms[i]); //output for testing only (remove it later)
}
else if(i > 1){ // process the itemIDs
int itemID = Integer.parseInt(terms[i]);
System.out.println(itemID); //output for testing only (remove it later)
}
}//end for
}input.close();
fileInput.close();
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.