FIF's 코딩팩토리

자바 기초 NCS교육과정(41)-스트림 문제풀이 본문

Back-End/국비 NCS교과과정

자바 기초 NCS교육과정(41)-스트림 문제풀이

FIF 2019. 8. 1. 16:40
반응형

이번 시간은 문제풀이 시간 입니다.

코드를 바로 보지 마시고, 충분한 시간을 가지고 고민해 보세요!

생각하는 시간이 많을수록 실력은 향상됩니다.

 

 

 

 

Question 1) Prob01_stream.java

 

 [문제] 
  input.txt 파일에는 팝송 가사가 들어있다. 
  이 파일에서 검색하고자 하는 문자열이 포함되어있는 라인의 번호와 
  가사를 콘솔에 출력하는 search(String inputFile, String searchWord) 
  메서드를 구현하시오.
   
  [프로그램 실행결과]
  5 line : It exists to give You comfort
  6 line : It is there to keep you warm
  9 line : When You are most alone
  10 line : The memory of love will bring you home
  14 line : It invites you to come closer
  15 line : It wants to show you more
  17 line : And even if you lose yourself
  20 line : will see you through
  39 line : My memories of love will be of you

 

input.txt

John Denver & Placido Domingo

Perhaps love is like a resting place
A shelter from the storm
It exists to give You comfort
It is there to keep you warm

And in those times of trouble
When You are most alone
The memory of love will bring you home

Perhaps love is like a window
Perhaps an open door
It invites you to come closer
It wants to show you more

And even if you lose yourself
And don't know what to do
The memory of love
will see you through

Oh love to some is like a cloud
To some as strong as steel
For some a way of living
For some a way to feel

And some say love is holding on
And some say letting go
And some say love is everything
Some say they don't know

Perhaps love is like the ocean
Full of conflict, full of pain
Like a fire when it's cold outside
Thunder when it rains

If I should live forever
And all my dreams come true
My memories of love will be of you
public class Prob01_stream {
	public static void main(String[] args) throws Exception {

		search(".\\src\\java1011_stream\\answ\\input.txt", "You");
	}// end main()

	private static void search(String inputFile, String searchWord) {
		FileReader fr=null;
		LineNumberReader re=null;
		
        //구현하세요
			
			



	}// end search()
}// end class

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Answer 1) prob01_stream.java

public class Prob01_stream {
	public static void main(String[] args) throws Exception {

		search(".\\src\\java1011_stream\\answ\\input.txt", "You");
	}// end main()

	private static void search(String inputFile, String searchWord) {
		FileReader fr=null;
		LineNumberReader re=null;
		try {
			 fr = new FileReader(inputFile);
			 re = new LineNumberReader(fr);
			String line="";
         
			while ((line=re.readLine()) != null) {
				//if(line.toUpperCase().matches(".*"+searchWord.toUpperCase()+".*"))
				if(line.toLowerCase().contains(searchWord.toLowerCase()))
				System.out.printf("%d : %s\n",re.getLineNumber(),line);
			}
			
			

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				re.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}

	}// end search()
}// end class

출력결과

 

 

Question 2) Prob02_stream.java

 

[문제]
   sun.txt파일에서 데이터를 읽어온후 ‘\t’와 ‘ ‘을 ‘-‘ 로 변환하여
   프로그램을 구현하시오.
     
  [프로그램 실행결과]
  hello-world-!!!
  java-programming
  jsp-servlet-programming!

 

 

sun.txt

hello world !!!
java programming
jsp servlet programming!

 

public class Prob02_stream {
	public static void main(String args[]) {
		String[] lines = readLines(".\\src\\java1011_stream\\prob\\sun.txt");
		
		for (int i = 0; i < lines.length; i++) {
			printLine(lines[i]);
		}
	}

	public static String[] readLines(String fileName) {
		/*
		 * 파라미터로 전달받은 txt파일을 읽어 들여, 파일의 줄 수에 해당하는 String[]을 생성하여 해당 String[]에 한
		 * 라인씩 저장해서 반환한다.
		 */

		return null;
	}// end readLines()

	public static void printLine(String line) {
		/*
		 * 문자열을 받아들여 ‘\t’와 ‘ ‘을 ‘-‘ 로 변환하여 콘솔에 출력한다.
		 */
		
		
	}// end printLine()
}// end class

 

 

 

 

 

Answer 2) prob02_stream.java

public class Prob02_stream {
	public static void main(String args[]) {
		String[] lines = readLines(".\\src\\java1011_stream\\answ\\sun.txt");

		for (int i = 0; i < lines.length; i++) {
			printLine(lines[i]);
		}
	}

	public static String[] readLines(String fileName) {
		/*
		 * 파라미터로 전달받은 txt파일을 읽어 들여, 파일의 줄 수에 해당하는 String[]을 생성하여 해당 String[]에 한
		 * 라인씩 저장해서 반환한다.
		 */

		FileReader fr = null;
		BufferedReader br = null;
		String[] sn = null;
		File file = new File(fileName);
		try {
			fr = new FileReader(file);
			br = new BufferedReader(fr);

			Stream<String> aa = br.lines();
			Object[] line = aa.toArray();
			sn = new String[line.length];
			for (int i = 0; i < sn.length; i++)
				sn[i] = (String) line[i];
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sn;
	}// end readLines()

	public static void printLine(String line) {
		/*
		 * 문자열을 받아들여 ‘\t’와 ‘ ‘을 ‘-‘ 로 변환하여 콘솔에 출력한다.
		 */

		System.out.println(line.replaceAll("[\t ]", "-"));
	}// end printLine()
}// end class

 

출력결과

 

 

 

 

반응형
Comments