Created
February 14, 2012 23:36
-
-
Save thomashw/1831663 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* To use Scanner class */ | |
import java.util.Scanner; | |
/* To write to file */ | |
import java.io.*; | |
public class EasyChallengeOne | |
{ | |
private String name; | |
private String age; | |
private String username; | |
private static final String logFileName = "log.txt"; | |
public EasyChallengeOne() | |
{ | |
name = new String(); | |
age = new String(); | |
username = new String(); | |
} | |
public void getUserDetails() | |
{ | |
Scanner input = new Scanner( System.in ); | |
/* Ask for the user's name */ | |
System.out.println( "What's your name?" ); | |
/* Get the response */ | |
name = input.next(); | |
/* Ask for the user's age */ | |
System.out.println( "What's your age?" ); | |
/* Get the response */ | |
age = input.next(); | |
/* Ask for the user's username */ | |
System.out.println( "What's your username?" ); | |
/* Get the response */ | |
username = input.next(); | |
} | |
public String toString() | |
{ | |
return "Your name is " + name + ", you are " + age + ", and your username is " + username + "."; | |
} | |
public void logUserDetails() throws IOException | |
{ | |
File file = new File( logFileName ); | |
boolean fileCreated = file.createNewFile(); | |
if( !fileCreated ) { | |
System.err.println( "Error: log file already exists." ); | |
return; | |
} | |
FileWriter fstream = new FileWriter( logFileName ); | |
BufferedWriter out = new BufferedWriter( fstream ); | |
out.write( this.toString() ); | |
out.close(); | |
} | |
public static void main( String[] args ) throws IOException | |
{ | |
EasyChallengeOne easy = new EasyChallengeOne(); | |
easy.getUserDetails(); | |
System.out.println( easy.toString() ); | |
easy.logUserDetails(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment