Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class StudentNames
- {
- private ArrayList<String> names;
- private int numOfStudents;
- public StudentNames(int numOfStudents)
- {
- names = new ArrayList<String>(3);
- this.numOfStudents = numOfStudents;
- }
- void getName() throws Exception {
- Scanner cin = new Scanner(new InputStreamReader(System.in));
- for (int i=0; i<numOfStudents; i++) {
- System.out.println("Student number " + (i+1) + "'s name please!");
- names.add(cin.next());
- }
- System.out.println("Thanks for the names!");
- }
- void writeToScreen() throws Exception {
- int i=0;
- for(String s: names)
- System.out.println( (i+1) + ": " + s );
- }
- void writeToFile(String filename) throws Exception {
- PrintWriter writer = new PrintWriter(filename);
- for(String s: names)
- writer.println(s);
- }
- public static void main(String[] args) throws Exception {
- StudentNames sn = new StudentNames(3);
- try{
- sn.getName();
- sn.writeToScreen();
- sn.writeToFile("abc.txt");
- }catch(IOException e){
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement