Sign in to follow this  

Why won't it go to the method?

This topic is 1928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

[code]Leisure
Force
Point
Phile
Cloaked
Saint
Death
Zodiak
Fiasco
Undead
Menacing
Final
Autophagy
Dark
Matter[/code]

Thanks! Edited by LeisureForce

Share this post


Link to post
Share on other sites
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:

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

Hope that helps you along!

Share this post


Link to post
Share on other sites
Sign in to follow this