[ 작업 환경 ]

 

Windows10

jdk-15

 

 

 

 

 

 [ 문제 ]

 

HW8.zip
0.01MB

 

PART #1 
•	첨부로 제공된 mydata.txt 파일과 Car.java 파일을 참고하시오.
•	mydata.txt에 각각의 row 마다 Car 인스턴스의 정보를 담고 있다.
•	mydata.txt 에 정보를 사용하여 Car[]를 만들고, 정보를 화면에 출력하시오.
•	mydata.txt의 각 줄은 아래와 같은 comma으로 띄어지며 공백은 무시하도록 처리할 수 있어야 한다.
ray,40,  230,red
ray, 40, 230,  red
•	mydata.txt의 각 줄은 아래와 같이 integer 정보가 아닌 정보가 들어온 경우에는 그 줄은 무시하고 처리할 수 있어야 한다.
ray,40, rr,red
•	각 Car의 speed를 10만큼 증가시키고, mileage를 100만큼 증가시켜서 화면에 출력하고, Car 정보를 mydataOut.txt에 저장하시오.
•	아래의 실행을 참고하시오.
D:\HW8>javac prob1/*.java

D:\HW8>java prob1.CarArrayTest
------- before
name:ray speed: 40 mileage: 230 color: red
name:pride speed: 20 mileage: 30000 color: white
name:sonota speed: 40 mileage: 5000 color: black
name:k5 speed: 70 mileage: 1000 color: green
name:ray2 speed: 40 mileage: 230 color: red
name:pride2 speed: 20 mileage: 30000 color: white
name:sonota2 speed: 40 mileage: 5000 color: black
name:k52 speed: 70 mileage: 1000 color: green
name:ray3 speed: 40 mileage: 230 color: red
name:pride3 speed: 20 mileage: 30000 color: white
name:sonota3 speed: 40 mileage: 5000 color: black
name:k53 speed: 70 mileage: 1000 color: green
name:ray4 speed: 40 mileage: 230 color: red
name:pride4 speed: 20 mileage: 30000 color: white
name:sonota4 speed: 40 mileage: 5000 color: black
name:k54 speed: 70 mileage: 1000 color: green
name:ray5 speed: 40 mileage: 230 color: red
name:pride5 speed: 20 mileage: 30000 color: white
name:sonota5 speed: 40 mileage: 5000 color: black
name:k55 speed: 70 mileage: 1000 color: green
------- after
name:ray speed: 50 mileage: 330 color: red
name:pride speed: 30 mileage: 30100 color: white
name:sonota speed: 50 mileage: 5100 color: black
name:k5 speed: 80 mileage: 1100 color: green
name:ray2 speed: 50 mileage: 330 color: red
name:pride2 speed: 30 mileage: 30100 color: white
name:sonota2 speed: 50 mileage: 5100 color: black
name:k52 speed: 80 mileage: 1100 color: green
name:ray3 speed: 50 mileage: 330 color: red
name:pride3 speed: 30 mileage: 30100 color: white
name:sonota3 speed: 50 mileage: 5100 color: black
name:k53 speed: 80 mileage: 1100 color: green
name:ray4 speed: 50 mileage: 330 color: red
name:pride4 speed: 30 mileage: 30100 color: white
name:sonota4 speed: 50 mileage: 5100 color: black
name:k54 speed: 80 mileage: 1100 color: green
name:ray5 speed: 50 mileage: 330 color: red
name:pride5 speed: 30 mileage: 30100 color: white
name:sonota5 speed: 50 mileage: 5100 color: black
name:k55 speed: 80 mileage: 1100 color: green
------- show mydataOut.txt
ray,50, 330,red
pride,30, 30100,white
sonota,50, 5100,black
k5,80, 1100,green
ray2,50, 330,red
pride2,30, 30100,white
sonota2,50, 5100,black
k52,80, 1100,green
ray3,50, 330,red
pride3,30, 30100,white
sonota3,50, 5100,black
k53,80, 1100,green
ray4,50, 330,red
pride4,30, 30100,white
sonota4,50, 5100,black
k54,80, 1100,green
ray5,50, 330,red
pride5,30, 30100,white
sonota5,50, 5100,black
k55,80, 1100,green

D:\HW8>

 

 

 

 

1. CarArrayTest.java 작성

 

package prob1;

import java.io.*;

public class CarArrayTest {
		public static void main(String[] args) {
		Car[] cars = MyTextDataReader.readText("mydata.txt");
		System.out.println("------- before");
		for (int k=0; k<cars.length; k++)
		{
			System.out.println("" + cars[k]);
		}
		
		System.out.println("------- after");
		// loop to increment speed by 10 and mileage by 100 for all cars
		for(int k=0;k<cars.length;k++)
		{
			cars[k].speed += 10;
			cars[k].mileage += 100;
		}
		
		// save updated car information to mydataOut.txt
		try {
			// open the file in write mode
			FileWriter writer = new FileWriter("mydataOut.txt");
			// loop over the cars array
			for(int i=0;i<cars.length;i++) {
				// output the name, speed, mileage and color separated by comma
				writer.write(cars[i].name+","+cars[i].speed+", "+cars[i].mileage+","+cars[i].color);
				if(i < cars.length-1) // this is not the last record, output a new line
					writer.write("\n");
			}
				writer.close(); // close the file

		} catch (IOException e) {
			e.printStackTrace();
		}
		
		// loop to output the data of cars to console
		for (int k=0; k<cars.length; k++)
		{
		System.out.println("" + cars[k]);
		}
		
		// display the details of mydataOut.txt file
		System.out.println("------- show mydataOut.txt");
		MyTextDataReader.showTextfile("mydataOut.txt");
		}
}


 

 

 

 

 

2. MyTextDataReader.java 작성

 

package prob1;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.*;

public class  MyTextDataReader
{
	public static void main(String[] args) {
	MyTextDataReader.showTextfile("mydata.txt");
	}

	public static Car[] readText(String fileName) {
		Car[] cars = new Car[0];
		try {
			// open the input file
			FileInputStream fstream = new FileInputStream(fileName);
			BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
			String strLine;
			String name, color;
			int speed, mileage;
			//Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				// split the line using comma as the delimiter
				String[] data = strLine.split(",");


				try {
					// get the fields from the line and convert integer data from string to integer, removing leading and trailing space from strings
					name = data[0].trim();
					speed = Integer.parseInt(data[1].trim());
					mileage = Integer.parseInt(data[2].trim());
					color = data[3].trim();

					// expand the cars array by 1
					cars = Arrays.copyOf(cars, cars.length+1);
					// create a new object and insert it at the end of cars array
					cars[cars.length-1] = new Car(name, speed, mileage, color);

				} catch(NumberFormatException e) {
					// executed when integer input is not integer
				}
			}

			//Close the input stream
			fstream.close();

		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}

		return cars;
	}

	public static void showTextfile(String fileName) {	  
		try{	
			FileInputStream fstream = new FileInputStream(fileName);
			BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
			String strLine;
		  
			//Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				System.out.println(strLine);
			}
			//Close the input stream
			fstream.close();
		} catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}


}

 

 

 

 

 

3. 소스 실행 예시

 

javac prob1/*.java
java prob1.CarArrayTest

 

 

 

 

 

 

 

 

 

+ Recent posts