Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why won't it go to the method?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 LeisureForce   Members   -  Reputation: 119

Like
0Likes
Like

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. 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!

Edited by LeisureForce, 06 October 2012 - 12:56 AM.


Sponsor:

#2 Dennieh   Members   -  Reputation: 612

Like
3Likes
Like

Posted 06 October 2012 - 01:31 AM

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!

#3 LeisureForce   Members   -  Reputation: 119

Like
0Likes
Like

Posted 06 October 2012 - 11:42 AM

It worked! I'm coming from Python, so I guess it's a little different. Thanks for your help!

#4 SimonForsman   Crossbones+   -  Reputation: 5753

Like
0Likes
Like

Posted 06 October 2012 - 01:06 PM

You can also use equalsIgnoreCase (to catch for example both "a" and "A" with the same comparison)
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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS