Why won't it go to the method?

Started by
2 comments, last by SimonForsman 11 years, 6 months ago
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!
Advertisement
Hi!

I haven't tried running your code, but one pretty obvious problem is using the equals operator (==) for comparing strings. This doesn't quite work as you'd expect in Java. What's happening is that you are comparing the memory locations of these strings, not the actual content. Which probably isn't what you intended. Instead, try using String's equals function, like so:


if (command.equals("g")){
getNames();
}
if (command.equals("a")){
addToList();
}
if (command.equals("q")){
isRunning = false;
System.exit(0);
}


Hope that helps you along!
It worked! I'm coming from Python, so I guess it's a little different. Thanks for your help!
You can also use equalsIgnoreCase (to catch for example both "a" and "A" with the same comparison)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement