Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualLeisureForce

Posted 06 October 2012 - 12:56 AM

Hey all, I have been working on a username generator as some practise in java. But I have come into some issues. When I take input from the user on which function they'd like to go to, the program just stops. And how would I go about picking 2 random lines from the NameList.txt so I can pair them up to make a name. Also, can you tell me if this is the right way to go about reading/writing to my code.

[source lang="java"]import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataInputStream;import java.io.FileInputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.Scanner;public class UsernameGen { public Random randomNumber = new Random(); public List<String> userNames = new ArrayList<String>(); public static Scanner scanner = new Scanner(System.in); public boolean isRunning = true; public String command; public UsernameGen(){ home(); } public void home(){ System.out.println("--(A)dd a name to the list. "); System.out.println("--(G)enerate a name. "); System.out.println("--(Q)uit. "); System.out.println("Enter a command: "); command = scanner.next(); if (command == "g"){ getNames(); } if (command == "a"){ addToList(); } if (command == "q"){ isRunning = false; System.exit(0); } } public void addToList(){ String nameToAdd; System.out.println("What name would you like to add? "); nameToAdd = scanner.nextLine(); FileWriter fstream; try { fstream = new FileWriter("NameList.txt", true); BufferedWriter out = new BufferedWriter(fstream); out.write(nameToAdd); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } home(); } public void getNames(){ try { BufferedReader in = new BufferedReader(new FileReader("NameList.txt")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } catch (IOException e) { } home(); } public static void main(String[] argsv){ UsernameGen usng = new UsernameGen(); }}[/source]


And the NameList.txt

Leisure
Force
Point
Phile
Cloaked
Saint
Death
Zodiak
Fiasco
Undead
Menacing
Final
Autophagy
Dark
Matter

Thanks!

#1LeisureForce

Posted 06 October 2012 - 12:54 AM

Hey all, I have been working on a username generator as some practise in java. But I have come into some issues. When I take input from the user on which function they'd like to go to, the program just stops. Also, can you tell me if this is the right way to go about reading/writing to my code.

[source lang="java"]import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataInputStream;import java.io.FileInputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.Scanner;public class UsernameGen { public Random randomNumber = new Random(); public List<String> userNames = new ArrayList<String>(); public static Scanner scanner = new Scanner(System.in); public boolean isRunning = true; public String command; public UsernameGen(){ home(); } public void home(){ System.out.println("--(A)dd a name to the list. "); System.out.println("--(G)enerate a name. "); System.out.println("--(Q)uit. "); System.out.println("Enter a command: "); command = scanner.next(); if (command == "g"){ getNames(); } if (command == "a"){ addToList(); } if (command == "q"){ isRunning = false; System.exit(0); } } public void addToList(){ String nameToAdd; System.out.println("What name would you like to add? "); nameToAdd = scanner.nextLine(); FileWriter fstream; try { fstream = new FileWriter("NameList.txt", true); BufferedWriter out = new BufferedWriter(fstream); out.write(nameToAdd); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } home(); } public void getNames(){ try {     BufferedReader in = new BufferedReader(new FileReader("NameList.txt"));     String str;     while ((str = in.readLine()) != null) {         System.out.println(str);     }     in.close(); } catch (IOException e) { } home(); } public static void main(String[] argsv){ UsernameGen usng = new UsernameGen(); }}[/source]

And the NameList.txt

Leisure
Force
Point
Phile
Cloaked
Saint
Death
Zodiak
Fiasco
Undead
Menacing
Final
Autophagy
Dark
Matter

Thanks!

PARTNERS