Click here to Skip to main content
15,888,158 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have code that I need to refactor to have one main and multiple methods. Anyone out there that can help with a very simple answer?

Java
public class Quiz {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		String inputString = null;
		String outputString = null;

		// accept string from user
		System.out.print("User input ");
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			inputString = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println(inputString);

		// write string to disk file named data.dat
		try {
			bw = new BufferedWriter(new FileWriter("data.dat"));
			bw.write(inputString);
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		// read the string back in from the file
		try {
			br = new BufferedReader(new FileReader("data.dat"));
			inputString = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// display to the screen
		System.out.println(inputString);
		
	}// main

}// class
Posted
Updated 18-Aug-11 18:17pm
v4
Comments
Sergey Alexandrovich Kryukov 18-Aug-11 22:09pm    
Answer to what? There is no question.
--SA

If you are specifically looking for Refactoring methodology, you can refer 'Refactoring' book by Martin Fowler. Useful stuff!
 
Share this answer
 
well, first of all, i hope you are not expecting anyone here to do it for you, but what you have to do is basically something like this:

public class Quiz {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
			
			string readString = ReadInput();
			System.out.println(readString);
			
			StoreToFile(readString);
			
			string readFromFileString = ReadFromFile();
			System.out.println(readFromFileString);
			
	}// main
	
	private string ReadInput()
	{	BufferedReader br = null;
		String inputString = null;
 
		// accept string from user
		System.out.print("User input ");
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			inputString = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return inputString;
	}
	
	private void StoreToFile(string inputString)
	{
		
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter("data.dat"));
			bw.write(inputString);
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private string ReadFromFile()
	{
		BufferedReader br = null;
		String inputString = null;
		// read the string back in from the file
		try {
			br = new BufferedReader(new FileReader("data.dat"));
			inputString = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// display to the screen
		return inputString;
	}

}// class


and as i said before, im not going to do it for you, but this should give you a very clear idea what you need to do, those are just a quick edit of the code that you posted, so im not sure it can even be compiled, and it could be implemented better, but the general idea of what you have to do is:

- a method that read user input from keyboard and return a string
- a method that takes a string as a parameter and write it to a file
- a method that read input from a file and return it
- and finally call the methods from your main

p.s: just wondering, did u learnt about exception before you understand method? i see you are using the try/catch blocks pretty often, u should be able to do this easily..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900