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...
Left shift. I am trying to left shift a string located in a text file. I...
Left shift. I am trying to left shift a string located in a text file. I am using c++ and the goal is to left shift by 1 character a string of length N, and output on stdout all of the performed shifts. Can someone tell me what I am doing wrong, and tell me what I can fix? i.e: text file contains: Hi there!. Output on stdout: Hi there!, i there!H, _there!Hi, there!Hi_,...., !Hi_there. #here "_" represent space. -------------------------------------------------------------------------------------------------------------------------...
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 struggling with this java code ublic class HW2_Q4 {    public static void main(String[]...
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[] args) {        // Define the input string. Note that I could have written it all on a        // single line, but I broke it up to match the question.        String input = "Book , Cost , Number\n"                    + "Hamlet , 24.95 , 10\n"                    + "King Lear , 18.42...
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 trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT