Question

how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException;...

how would i use test.add because i would like to use add
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class checkFruit {

    private List<String> tests;

    public List<String> getFruit(String fruits) {
        tests = new ArrayList<String>(Arrays.asList(fruits.split(",")));
        for (String test : tests) {
            System.out.println(test);
            tests.add()//how would i use add here
        }
        return tests;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter file name: ");
        File file = new File(in.nextLine());
        try {
            checkFruit fruit = new checkFruit();
            System.out.println(fruit.getFruit("Apple,Banana,Pineapple"));
            Scanner fin = new Scanner(file);
            while (fin.hasNextLine()) {
                System.out.println(fruit.getFruit(fin.nextLine()));
            }
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " is not found!");
        }
        in.close();
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class checkFruit {

    private List<String> tests;

    public List<String> getFruit(String fruits) {
        tests = new ArrayList<String>(Arrays.asList(fruits.split(",")));
        for (String test : tests) {
            System.out.println(test);
            tests.add()//how would i use add here
        }
        return tests;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter file name: ");
        File file = new File(in.nextLine());
        try {
            checkFruit fruit = new checkFruit();
            Scanner fin = new Scanner(file);
            List<String> fruits = new ArrayList<>();
            while (fin.hasNextLine()) {
                fruits.addAll(fruit.getFruit(fin.nextLine()));
            }
            // add other fruits
            fruits.add("Mango");
            fruits.add("Banana");
            System.out.println("Fruits are: " + fruits);
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " is not found!");
        }
        in.close();
    }
}
Add a comment
Know the answer?
Add Answer to:
how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • how would i do subdirectory because i want to be able to grab all subdirectory out...

    how would i do subdirectory because i want to be able to grab all subdirectory out of a directory and still be able to get add them I have a subdirectory called testFolder in it contains the following: defaulfolder.1 defaultfolder defaultfolder2.1 defaultfolder2 I only want to be able to grab the default folder without the "." i would like to get a test output of the answers import java.io.File; import java.util.ArrayList; import java.util.List; public class checkFile { private List<File> tests;...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Finds the specified set of words in the specified file and returns them * as an ArrayList. This finds the specified set in the file which is on the * line number of the set. The first line and set in the file is 1. * * This returns an ArrayList with the keyword first, then : and then followed...

  • Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File;...

    Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Lab8Num1 { public static void main(String[] args) { //Declaring variable to be used for storing and for output double biggest,temp; //Creating file to read numbers File inFile = new File("lab8.txt"); //Stream to read data from file Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { //Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex); } //get first number...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */    public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {   ...

    /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {    /**    * @param args    */    public static void main(String[] args) {        List groceryList = new ArrayList<>();        groceryList.add("rice");        groceryList.add("chicken");        groceryList.add("cumin");        groceryList.add("tomato");        groceryList.add("cilantro");        groceryList.add("lime juice");        groceryList.add("peppers");               groceryList.remove("cilantro");               for (int i = 0; i            if (groceryList.get(i).equals("peppers")) {...

  • Can anyone debug this for me please? Java code to copy package debugmeone; import java.io.File; import...

    Can anyone debug this for me please? Java code to copy package debugmeone; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadTextFile { private Scanner input; // Ignore the hint given by NetBeans public void openFile() { try { input = new Scanner( new File("accountrecords.txt")); } catch(Exception e) { System.out.println("Something bad just happened here."); System.exit(707); } catch( FileNotFoundException fnfe) { System.out.println("Error - File Not Found: accountrecords.txt"); System.exit(100); } } } ITCS-2590 Debugging Execse Chapter 11 - NetBeans IDE 8.2 File...

  • Fibtester: package fibtester; // Imported to open files import java.io.File; // Imported to use the exception...

    Fibtester: package fibtester; // Imported to open files import java.io.File; // Imported to use the exception when files are not found import java.io.FileNotFoundException; // Imported to write to a file import java.io.PrintWriter; // Imported to use the functionality of Array List import java.util.ArrayList; // Imported to use the exceptions for integer values import java.util.NoSuchElementException; // Imported to use when the array is out of bounds import java.lang.NullPointerException; // Imported to take input from the user import java.util.Scanner; public class FibTester...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT