Question

In: Computer Science

/** * Loads a list of items from the given file. Each line is in the...

/**
* Loads a list of items from the given file. Each line is in the format
* <item type>~<item data>. The createFromString() methods in the item
* classes will be used to parse the item data.
* @param filename the filename
* @return the list of items
* @throws IOException if the file can't be opened
*/
public static List<Item> loadItems(String filename) throws IOException {
List<Item> items = new ArrayList<>();
Scanner inPut = new Scanner(new FileReader(filename));
       while(inPut.hasNextLine()) {
           String line = inPut.nextLine();
           String [] tokens = line.split(":");
           String type = tokens[0];
           String data = tokens[1];
           items.add(type,data);
       }
return items;
}
}

can you please check this line " items.add(type,data);" . I am having problem with it

Solutions

Expert Solution

Note:

1.)We can't write List generic type as class name.

2.)We can't add two arguments ex:add(data,type)

we can add only one item by uisng add() Method, ex:add(data). If you want add multiple items then you can use addAll() method

Code:

package assignment;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Item {
   public static List<String> loadItems(String filename) throws IOException {
       List<String> items = new ArrayList<>();
       Scanner inPut = new Scanner(new FileReader(filename));
       while(inPut.hasNextLine()) {
       String line = inPut.nextLine();
       String [] tokens = line.split(":");
       String type = tokens[0];
       String data = tokens[1];
       items.add(type);
       items.add(data);
       }     
       return items;
       }

   public static void main(String[] args) throws IOException {
       List list=loadItems("abc.file");
       list.forEach(System.out::println);

   }

}

Note:****I hope your happy with my answer***If you have any doubts please comment me****Thank you.....


Related Solutions

in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Assume there is a file called "mydata". each line of the file contains two data items
how do you read in a file in JAVA Assume there is a file called "mydata". each line of the file contains two data items: hours and rate. hours is the represented by the number of hours the worker worked and rate is represented as hourly rate of pay. The first item of data is count indicating how many lines of data are to follow.Methodspay- accepts the number of hours worked and the rate of pay. returns the dollor and cents...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
Given a list of items, write a program that generates a list of lists of the...
Given a list of items, write a program that generates a list of lists of the following form: [a,b,c,...,z]⇒[[z], [y,z], [x,y,z], ... , [a,b, ... ,y,z]] Hint: Slicing is your friend. please write a python program
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
Given the following list of items: a. Classify the items as A, B, or C b....
Given the following list of items: a. Classify the items as A, B, or C b. Determine the economic order quantity for each item (round to the nearest whole unit) Item Estimated Annual Demand Ordering Cost Holding Cost (%) Unit Price H4-010 20,000 55 25 4.5 H5-201 60,200 65 25 6 P6-400 9,800 85 35 30.5 P6-401 14,500 55 35 14 P7-100 6,250 55 35 11 P9-103 7,500 55 45 24 TS-300 21,000 45 30 47 TS-400 45,000 45 30...
(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
For each of the following companies, calculate the cash flow from operations: (List items that increase...
For each of the following companies, calculate the cash flow from operations: (List items that increase cash flow first.) NuVu Ltd. ABC Inc. Akhtar Ltd. Sales revenue $381,000 $577,000 $936,000 Cost of goods sold 210,000 330,000 621,000 Selling and administrative expenses 65,000 94,000 111,000 Depreciation expense 6,600 18,000 28,000 Interest expense 3,000 1,000 2,000 Income tax expense 18,000 35,000 45,000 Dividends paid 7,000 5,000 25,000 Increase/ (Decrease) in Accounts receivable (2,500 ) 6,000 (9,500 ) Inventory 5,800 (8,000 ) 17,000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT