Question

java question ,show code and explain! I have a file call input.txt contain: Boston (MA), New...

java question ,show code and explain!

I have a file call input.txt contain:

Boston (MA), New York City (NY, CT, NJ)
New York City (NY, CT, NJ), Philadelphia (PA, NJ)
Philadelphia (PA, NJ), Baltimore (MD)
Baltimore (MD), Washington (DC)
Washington (DC), Charlotte (NC)

My code:

public static void main(String[] args) {

   File file = new File("input.txt");
   String[] array;
       try {
           Scanner in = new Scanner(file);
           Graph graph = new Graph();
   while (in.hasNextLine()) {
       String key = in.nextLine();
       array = key.split("please help here"); // add something for me.
       System.out.println(array[0] + " " + array[1]);
  
}

   }catch(FileNotFoundException e){
           e.printStackTrace();
       }
}

The output I want is

Boston (MA) New York City (NY, CT, NJ)

New York City (NY, CT, NJ) Philadelphia (PA, NJ)

Philadelphia (PA, NJ) Baltimore (MD)

Baltimore (MD) Washington (DC)
Washington (DC) Charlotte (NC)

Homework Answers

Answer #1

Let me give an overview about reading the files from the txt file and printing the content into the console. It can be done in many ways, but the efficient way to read a file is to use the BufferedReader which elaborates them in reading the large text files content by ranging from MB's to GB's in size.

The program which you have provided is not finding the desired path of a input.txt file, As per Java Community standards, The path should be used is Absolute i.e, for example:- F:\\javaFiles\\input.txt.

Code:-

import java.io.*;
class ReadContentFromFile {
public static void main(String args[]) {

try {

FileInputStream fis = new FileInputStream("F:\\javaFiles\\input.txt");
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
dis.close();
} catch (Exception exp) {

System.err.println("Error: " + exp.getMessage());

}
}
}

Know the answer?
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
Similar Questions
Here is data on annual rainfall in select east-coast cities. Construct a box plot. City Millimeters...
Here is data on annual rainfall in select east-coast cities. Construct a box plot. City Millimeters Washington, DC 1009 Philadelphia, PA 1055 Baltimore, MD 1064 Boston, MA 1112 Raleigh, NC 1169 Tampa, FL 1176 Virginia Beach, VA 1182 Providence, RI 1198 Nashville, TN 1200 Atlanta, GA 1263 New York, NY 1268 Orlando, FL 1289 Jacksonville, FL 1331 Memphis, TN 1363 Miami, FL 1572 What is the lower fence: ______ What is the upper fence: ______ Draw a box plot (clearly...
Do it in python please Boston Americans 1903 No Winner 1904 New York Giants 1905 Chicago...
Do it in python please Boston Americans 1903 No Winner 1904 New York Giants 1905 Chicago White Sox 1906 Chicago Cubs 1907 Chicago Cubs 1908 Pittsburgh Pirates 1909 Philadelphia Athletics 1910 Philadelphia Athletics 1911 Boston Red Sox 1912 Philadelphia Athletics 1913 Boston Braves 1914 Boston Red Sox 1915 Boston Red Sox 1916 Chicago White Sox 1917 Boston Red Sox 1918 Cincinnati Reds 1919 Cleveland Indians 1920 New York Giants 1921 New York Giants 1922 New York Yankees 1923 Washington Senators...
Write a Python  program with correct indentation that creates a dictionary containing the U.S. states as keys...
Write a Python  program with correct indentation that creates a dictionary containing the U.S. states as keys and their abbreviations as values. The program should then randomly quiz the user by displaying the abbreviation and asking the user to enter that state's name. The program should keep a count of the number of correct and incorrect responses, as well as which abbreviation the user missed. You should include the following: Mainline logic and functions Error handling Dictionaries A file is attached...
This file contains data on the crime rates (total and violent) and population. Using Excel or...
This file contains data on the crime rates (total and violent) and population. Using Excel or other software of your choice, for each of the following pairs make a ScatterPlot then determine the Correlation Coefficient: (3pts) Crime Rate and Population (3pts) Violent Crime Rate and Population (3pts) Crime Rate and Population Density (that’s the Number of people Per square mile – Hint: You need to compute this last one on your own using data in the table.) Copy and paste...
Programming Language is Java In class you learned how to work with collections and streams. This...
Programming Language is Java In class you learned how to work with collections and streams. This lab allows you to apply the skills you learned to a list of riders. With your first two assignment complete, the coordinator at the Car Safety Academy decided to begin collecting names for a monthly mailing list. You've been asked to collect the names and perform some processing. First you will create a Riders class. Then you will create an array of riders with...
StatCrunch Project #1 Open “Cities”. Provide the following using Statcrunch: (in a pdf document) mean PM10...
StatCrunch Project #1 Open “Cities”. Provide the following using Statcrunch: (in a pdf document) mean PM10 median PM10 min PM10 max PM10 Q1 of PM10 Q3 of PM10 Standard deviation of PM10 Variance of PM10 PM10 stem & leaf plot PM10 histogram PM10 box plot PM10 normal quantile plot (QQ plot) Comparison box plots for REGION vs PM10 Scatterplot for Y vs MAX DEG F JAN What is the coefficient of linear correlation? Q3 Q1 city X Y State Region...
In R, if I have a plot of LIFE against BIRTH and I want to specifically...
In R, if I have a plot of LIFE against BIRTH and I want to specifically mark the data point corresponding to STATE "AK" in the graph as red colour(the rest of the points as black color), what R command should we use?(Please don't look through the data to find AK's birth value(24.8)or life value (69.31)and mark this point manually referring to this value as other state can have the same birth and life value) "STATE" "MALE" "BIRTH" "DIVO" "BEDS"...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT