Question

In: Computer Science

#Java I am reading from the txt file and I put everytihng inside of the String[],...

#Java

I am reading from the txt file

and I put everytihng inside of the String[], like that:

String[] values = str.split(", ");

But, now I have to retrieve the data from it one by one. How can I do it?

Here is the txt file:

OneTime, finish the project, 2020-10-15
Monthly, wash the car, 2020-11-10

Thanks!

Solutions

Expert Solution

Main.java :

import java.io.BufferedReader;
import java.io.FileReader;

/*
Java Program to read file and retrieve comma separated String 
*/
public class Main {
  public static void main(String[] args) throws Exception {
    /*
    Redaing file using BufferedReader 
    */
    BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    String line = null; // local variable to hold each line (it will change in each loop iteration)

    // while loop execute until no more line available to read in file.
    while ((line = br.readLine()) != null) {
      String[] values = line.split(","); // String array to hold each comma (,) separated string
      
      /*
      follwing for each loop retrieve the data from values(String array) one by one
      */
      for (String str : values) {
        System.out.println(str); // print each retrieved value in a single line
      }
    }
    br.close(); // close the BufferedReader.
  }
}

Sample Output :

Please refer to the Screenshot of the code given below to understand indentation of the code :


Related Solutions

JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to go...
This is my java method that is suppose to take data from a file(String, String, Double,...
This is my java method that is suppose to take data from a file(String, String, Double, Int ) and load that data into an object array. This is what I have and when I try to display the contents of this array it prints a "@" then some numbers and letters. which is not the correct output. any help correcting my method would be much appreciated. public void loadInventory(String fileName) { String inFileName = fileName; String id = null; String...
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
fp=open("us-counties.2.txt","r") #open file for reading fout=open('Linsey.Prichard.County.Seats.Manipulated.txt','w') #file for writting states=[i.strip() for i in fp.readlines()] #read the...
fp=open("us-counties.2.txt","r") #open file for reading fout=open('Linsey.Prichard.County.Seats.Manipulated.txt','w') #file for writting states=[i.strip() for i in fp.readlines()] #read the lines try: #aplit values and write into file a,b=state.split(',') print(a,b) #write into file fout.write(a+":"+b+"\n")\ except Exception#if we dont have two names (For a line like KENTUCKY or OHIO, It continues) Pass evaluate Data. Manipulation.py] Traceback (most recent call last): File "C:/Users/Prichard/Data. Manipulation.py", line 10, in <module> except Exception#if we dont have two names (For a line like KENTUCKY or OHIO, It continues) Syntax Error:...
I am a researcher interested in children’s reading. I feel children should be reading more as...
I am a researcher interested in children’s reading. I feel children should be reading more as they get older and feel generally interested in learning about current reading trends. I feel a good way to learn about the current reading trends to learn about their Accelerated Reader points. I feel older students are already reading more and should have more AR points than younger students. I predict that the older children will have more AR points than younger children. What...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
I am sure this is a silly question, but I was reading something that described the...
I am sure this is a silly question, but I was reading something that described the pre big-bang universe as having "nearly infinite mass." How can something be "nearly" infinite? The term seems to make no sense.
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
Python: The file, Program11.txt, on the I: drive contains a chronological list of the World Series’...
Python: The file, Program11.txt, on the I: drive contains a chronological list of the World Series’ winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note that the World Series was not played in 1904 or 1994. There are no entries in the file indicating this.) Write a program that reads this file...
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT