Slavi 94 Master Poster Featured Poster

Hey James, I got the GUI running now and it works pretty well. Now I want to add actual pictures where for each wrong letter new image would be added such as arms/legs etc. Any ideas where to start at?

Slavi 94 Master Poster Featured Poster

Oh, that makes sense! So just call tryLetter in action performed if I understood correct?

Slavi 94 Master Poster Featured Poster

Um, I changed the logic in a file and then the gui in another it looks like this currently

class HangmanLogic{
    private String[] words = {"first","second","third","fourth"}; 
    private static String wordToGuess;
    Random rnd = new Random();
    private static String letter;
    private int wrongGuesses = 0;
    private int guessedLetter;
    private ArrayList<String> al = new ArrayList<String>();


    HangmanLogic(){


        wordToGuess = words [rnd.nextInt(words.length)]; // Choose a random word from the list

    }

    public static void main (String[] args){
        HangmanLogic hm = new HangmanLogic();
        System.out.println(wordToGuess);
        hm.guessWord();
    }

    public static void setLetter(String s){
        letter = s;
    }

    public void guessWord(){
        for (int i = 0; i<wordToGuess.length();i++){ //create all letters represented by "_"
            al.add(i," _ ");
        }
        System.out.println(al);

        Scanner sc = new Scanner(System.in);

        while(wrongGuesses<5 && al.contains(" _ ")){ // If 5 mistakes or the word is completed
        letter = sc.next(); //get input

        if(wordToGuess.contains(letter)){ //does the word contain the input letter
            guessedLetter = wordToGuess.indexOf(letter); //location of the guessed letter
            al.set(guessedLetter,letter); //replace the "_" with the letter guessed
            System.out.println(al); //print the current state of the word
            if(!al.contains(" _ ")) System.out.println("Congratulations, you won!");
        }
        else {
            System.out.println("Wrong, try again! " +(wrongGuesses+1) +" out of 5 wrong attempts");
            wrongGuesses++;
            if(wrongGuesses>=5) System.out.println("Game Lost"); // game lost if 5 incorrect answers
        }

    }}
}

and the GUI class

class Hangman{
    private ArrayList<String> al = new ArrayList<String>();
    private JFrame frame = new JFrame("Hangman");
    private JTextArea wordToGuessGUI = new JTextArea(10,20);
    private JTextField usedLettersGUI = new JTextField(20);
    private JTextField letterGUI = new JTextField(1);
    private StringBuilder sbWordToGuess = new StringBuilder();
    private JTextArea finalMessage = new …
Slavi 94 Master Poster Featured Poster

Hm, makes it clearer to read?

Also, I have NPE in here ..

letterGUI.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                setLetter(letterGUI.getText());
                letterGUI.setText("");
            }
        });

I am setting what is entered in the GUI to the string that takes the input character by the user but if(wordToGuess.contains(letter)) has null at start( I think) and when I run it it gives NPE

How can I solve that?

Slavi 94 Master Poster Featured Poster

Hmm, in general is it a bad idea to use JTextArea for this? I need a way to set a letter on a position, like ArrayList's set.. Can I use the ArrayList for the GUI? JTextArea has insert but I think it inserts new item doesnt replaces a current one?

Slavi 94 Master Poster Featured Poster

How can I keep track of the unguessed letters yet in my JTextArea? In the non gui version I used an arraylist, worked fine but should I leave the arraylist and use it to track unguessed caracters ? The method is still console based, just started implementing the gui made the layout now connecting it. What I did so far was append the number of letters that the chosen word contains

public void guessWord(){
        for (int i = 0; i<wordToGuess.length();i++){ //create all letters represented by "_"
            al.add(i," _ ");
        }
        //System.out.println(al);
        for(String s: al){
            wordToGuessGUI.append(s);
        }

        Scanner sc = new Scanner(System.in);
        while(wrongGuesses<5 && al.contains(" _ ")){ // If 5 mistakes or the word is completed
        letter = sc.next(); //get input
        if(wordToGuess.contains(letter)){ //does the word contain the input letter
            guessedLetter = wordToGuess.indexOf(letter); //location of the guessed letter
            al.set(guessedLetter,letter); //replace the "_" with the letter guessed
            System.out.println(al); //print the current state of the word
            if(!al.contains(" _ ")) System.out.println("Congratulations, you won!");
        }
        else {
            System.out.println("Wrong, try again! " +wrongGuesses +" out of 5 wrong attempts");
            wrongGuesses++;
            if(wrongGuesses>=5) System.out.println("Game Lost"); // game lost if 5 incorrect answers
        }

    }}
Slavi 94 Master Poster Featured Poster

I hope you fixed what hericles referred to, next step is implement getters
public void getAccountNumber(){
return AccountNumber;}
so when getAccountNumber is called it would return the Account number of the object it was called by

Slavi 94 Master Poster Featured Poster

Not sure if this will help but a while ago I tried reading a file this is what worked for me

try (BufferedReader br = new BufferedReader(new FileReader("C:\\file.txt"))){
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

I guess you can try using the path like that

Slavi 94 Master Poster Featured Poster

Nevermind, solved :)

Slavi 94 Master Poster Featured Poster

Well, the title says it all. Imagine that I have HelloWorld.java , I want to create an executable jar from it. I know that if I use IDE such as eclipse I can create it there but how can you create it without that?

Slavi 94 Master Poster Featured Poster

What james said look Here

Slavi 94 Master Poster Featured Poster

Here, my suggestion is start with "Hello world", where you can see how to print messages to the user in the console, then ask the user to enter a number, a word and a sentance. This will give u the basic knowledge you need to know for doing the quiz in console(not sure if you meant console project or gui but I assume console).
Take a look at the method System.out.println()
also read about Scanner(used to get input from users)

Slavi 94 Master Poster Featured Poster

I don't see where you have defined handler, but in general yes .. I just used this in something similiar

for (int i = 0; i<values.length; i++){
            final JToggleButton tg = new JToggleButton(values[i]);
            tg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){}

Then just define what you want to happen when action is performed

Slavi 94 Master Poster Featured Poster

Doogledude123, can you give ideas for some more of the project's you've done? Some of those seem pretty cool and I am planning to dedicate my entire summer to java, i really want to try if not all atleast most of the ideas

Slavi 94 Master Poster Featured Poster

Okay .. I've been really busy irl lately, I am back on tract now, kept the thread opened until I get the idea to work. Here is what I did after fixing some of my code is to save the path in an array of integers and then print it out

public class multiplePaths {
    private static HashMap<Integer, ArrayList<Integer>> graph = new HashMap<Integer, ArrayList<Integer>>();
    private static ArrayList<Integer> visitedVertices = new ArrayList<Integer>();
    private static ArrayList<Integer> paths = new ArrayList<Integer>();

    public static void main(String[] args){

        ArrayList<Integer> al = new ArrayList<Integer>();
        al.add(3);
        al.add(4);
        graph.put(1, al);
        graph.put(2, new ArrayList<Integer>(Arrays.asList(4,5)));
        graph.put(3, new ArrayList<Integer>(Arrays.asList(1)));
        graph.put(4, new ArrayList<Integer>(Arrays.asList(1,2,7)));
        graph.put(5, new ArrayList<Integer>(Arrays.asList(2,6)));
        graph.put(6, new ArrayList<Integer>(Arrays.asList(5)));
        graph.put(7, new ArrayList<Integer>(Arrays.asList(7,4)));
        graph.put(8, new ArrayList<Integer>(Arrays.asList(9)));
        graph.put(9, new ArrayList<Integer>(Arrays.asList(8)));

        checkAndClear(1,6);
        //System.out.println(checkAndClear(1,2));
        //System.out.println(checkAndClear(1,7));
        //System.out.println(checkAndClear(7,7));
    }

    private static boolean isConnected(int vertex1, int vertex2) {
        visitedVertices.add(vertex1);
        if (graph.get(vertex1).contains(vertex2)) {
            paths.add(vertex2);
            System.out.println(paths);
            return true;
        }
        else if(vertex1 == vertex2){
            return true;
        }
        ArrayList<Integer> a = graph.get(vertex1);
        //System.out.println(a);
        for (int i = 0; i < a.size(); i++) {
            int vis = a.get(i);
            if(!visitedVertices.contains(vis)) {
                paths.add(vis);
                //System.out.println(vis);
                if (isConnected(vis, vertex2)) return true;
            }   
        }
        return false;
    }
    private static boolean checkAndClear(int vertex1, int vertex2){
        boolean result = isConnected(vertex1,vertex2);
        visitedVertices.clear();
        return result;
    }
}

Any suggestions on where to go from now on? the second method was meant just to clear the visitedVertices in case few tests were run back to back

Slavi 94 Master Poster Featured Poster

Start an android project, then set up layout and images as you want them to be, then when it comes to the part where you'll have to design listeners and changes, you can ask spesific questions, otherwise don't think anyone in here would just sit and write the code for you

Slavi 94 Master Poster Featured Poster

Hmm this is something really interesting to attemp tbh, james's idea is nice as well, first make sure that you can record voice and save it in a file. Once that is done, just replace instead of save to a file use sockets to send it. I'll bookmark this thread, seems really fun good luck if you try to attempt it

Slavi 94 Master Poster Featured Poster

I never said bfs/dfs was for finding shortest path? Said that when I mentioned Dijktra's algorithm :D thanks James i ll write the few missing lines then and see if it works the way i think

Slavi 94 Master Poster Featured Poster

WHat kind of stopping condition would I have? Even if it finds all posible moves it will just keep trying unless there is something, and can't really think of anything other than compare the size of all nodes and those to visited nodes? That should ensure that every three path is checked ye?

Slavi 94 Master Poster Featured Poster

Oh, and I think Dijkstra's algorithm is using BFS, so it doesn't really show all possible ways to distination just the shortest one :D

Slavi 94 Master Poster Featured Poster

Some time ago, I wrote some code, which uses DFS to find whether there is a path from a source vertex to a destination vertex. However, I am thinking now what if there is more than 1 possible way? What comes to my mind is save paths in an ArrayList as Arralist of integer and then for every path source-destionation check whether it was found already or not? Just like visitting the nodes, mark them as visited. Actually when I think about it, only visitted nodes should be enough won't even need to check whether paths are identical or noth since it won't repeat the visitted nodes path again, but it has to be done in a loop now, what should the stopping condition be? What I do now is using DFS, as soon as the path is found it returns true and the search stops (my dfs is decleared as boolean, just wanted to check whether path exists or not). I guess it has to be similiar to Dijkstra's algorithm for the shortest path if there is cost added between the edges but in this case to display all paths not to return the shortest one? Any suggestions on how to the logic behind not stopping to execute until no unchecked nodes exist? How about having an arraylist with all vertexes, and the arraylist which gets all visited, if i compare the size of those, it should loop until it has been to every vertex right? as in while(!(allVertex.size() …

Slavi 94 Master Poster Featured Poster

Java! :D

Slavi 94 Master Poster Featured Poster

I am currently using a Dell laptop for about 2,5 years now ... the only problem that I have with it is overheating, It really starts to burn quite fast :s and even if I not doing anything you can the fan going mad. From what I see Acer is decent(especially if you want to have 2nd OS such as Linux). When i tried running linux on my laptop i could feel the buttons started melting cuz of overheating within like 5 minutes after starting it :D But also I see lately maybe 4 out of 5 people around me having Lenovo Think Pad, they seem pretty satisfied with it

Slavi 94 Master Poster Featured Poster

In this line Link newLink= new Link(data, head); you create new link with 2 parameters, but your constructor takes only 1 of type int public Link (int d)

Slavi 94 Master Poster Featured Poster

I think compilers for new people if thats what he is looking for is something like Eclipse or Netbeats, I myself prefer Eclipse from those 2 but since I started using Linux lately I've been using Sublime text as text editor and execute my code in the terminal. Personally I think subline is just amazing, with a few minor glitches so to speak, as its being too smart in some moments :p

Slavi 94 Master Poster Featured Poster

I was doing something similiar in matlab, where a had a huge data file with few thousand rows and about 15 colomns. I had to filter it so for example if value in column1 is 2 and in column2 is 4 then return the number in column 3. I think this will work with you as well, where you check according to your source and the connection next to it. Does this answer your question?

Slavi 94 Master Poster Featured Poster

What exactly do you want? If you want the stated to be written so you can complete your homework without effort, I don't think that will happen. To begin with you can look at class declarations, learn about Object oriented programming.

class MyClass { }
will create a class with the name MyClass. Each class can have many fields which are decleared public/private/protected. In most cases you'll set them as private so its a good start. Each field has a type such as bool (boolean), int(integer), String etc. When you declear fields you choose their scope and give the variable a name such as int MyInt reserves a space in memory for an integer called MyInt. You can set it to any real value using = so MyInt = 0;
Each class has a number of methods. Such as for example private int getMyInt() which by declaration returns an int ( declared in the method body). If you want a method to not return anything you set it as void. Now, if we want our method to return the value of MyInt then in the method body we have return MyInt. I think this is a good start to begin with

Slavi 94 Master Poster Featured Poster

So what exactly is your question? What you specified is method declaration public String Duplicate(String str). For the letters you can separate the string into chars and count how many times each chart is in the string.
You can get the size of the string and the use a loop to get the chars out. Then simply compare the chars and if you find a match increase a counter number

Slavi 94 Master Poster Featured Poster

Hey guys, I ve been using elementary (luna) os distribution of linux and I am really satisfied with it .. the only problem that i am facing is when switching between application sounds .. I ll give you an example:
Using my headphones to speak on teamspeak and listen to music. Suddenly I decide that I want my music to be played through my loudspeakers(for example) and when I select that sound will be played through loudspeakers, teamspeaks's sound gets "forced" played through there as well until I go to teamspeak and set the settings again(where it still shows that my headseats are communication device). Any suggestions on this 1?

Slavi 94 Master Poster Featured Poster

I just saw someone doing a Combination Lock, the idea is to enter 3 numbers and if they match with the password, then you have gained permission. Pretty easy/simple and would definately be useful for people who just start

Slavi 94 Master Poster Featured Poster

^

Slavi 94 Master Poster Featured Poster

Get an idea of a starting point and the destination point.
Once at starting point you can navigate north/south etc to a new location, if the new location is not the destination use recursive call of the function until destination reached. As something cool would be to print out visited paths and print out the path from source to destination ;)

Slavi 94 Master Poster Featured Poster

Interesting task you have there :) I'd assume that you've got it running by now but this is what i'd try

public void actionPerformed(ActionEvent e){
                    bld = (e.getActionCommand());
                    sb.append(bld);
                    if (sb.length()== 3) {
                        input = sb.toString();
                        if (input.equals(pass)){
                            JOptionPane.showMessageDialog(frame,"Success");
                            frame.dispose();
                        }
                        else{
                            JOptionPane.showMessageDialog(frame, "Wrong!");
                            sb.setLength(0);
                            failedAttempts++;
                            if (failedAttempts == 3){
                                JOptionPane.showMessageDialog(frame, "Your have ran out of attempts!");
                                frame.dispose();
                            }
                        }
                    }
                  }
Slavi 94 Master Poster Featured Poster

Hey again James :) To begin with thanks for your reply!
I think there was a small misunderstanding :p at first my run() was not meant to be the run method of Runnable, but I placed the run to be activate on button click and since my run() has an infinitive while loop which is waiting for messages from the server, i could never leave the button click execution and my gui(if u remember the gui code i posted earlier yesterday) just stucked. So I had to implement Runnable and use this in a new thread, then it worked great. Also here is the solution, in case someone doesn't want to use resources on infinitive while loops rather than event triggered execution. Oh, and since my old run() throws IOE, and the run() in runnable doesn't ,the solution to this is just place your run() in a try/catch block, that will do it :) Here is the main and the run(). Thanks to everyone who replied and also thanks James=)

public static void main(String[] args) throws Exception {

        final newClient program= new newClient("localhost", 9000); //"vps.devrandom.dk"

         ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
              System.out.println("details accepted");
              details = true;
              ok.setEnabled(false);
              login.setEnabled(true);
              System.out.println("connected");
              defaultHost = tfServer.getText();
              defaultPort = Integer.parseInt(tfPort.getText());

              (new Thread(program)).start();
            }
        });
    }



 public void run() {
        try {
        //connected = true;
        Socket clientSocket = new Socket(defaultHost, defaultPort);
        BufferedReader inFromUser = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
        clientSocket.getInputStream()));
        while (true){
              modifiedSentence …
Slavi 94 Master Poster Featured Poster

Never mind, solved using actionlistener :p

Slavi 94 Master Poster Featured Poster

Ah the print lines were only for me to checked whether or not in enters the loops, and If i remove the not connected line, the loop just stops >.>
I have actionListener on my button and when the button is pressed it just changes the value of details to true and then my run() method executes, or in other words I don't want the run method to execute before I've pressed a button

Slavi 94 Master Poster Featured Poster

Okay, so I have the following

while(!connected){
            if (details){ 
            System.out.println("connected");
            defaultHost = tfServer.getText();
            defaultPort = Integer.parseInt(tfPort.getText());
            program.run();
            }

            System.out.println("Not connected");

        }

where details is true when a button is clicked. This works fine expect that my console is getting spammed by "Not connected" until I press the button and details is true. How can I keep my while loop (!connected) running if I don't have the line System.out.println("Not connected"); ? If I remove it the while loop only executes once ... I am trying to have something like Do nothing until details is true
Any help is appreciated ! :D

Slavi 94 Master Poster Featured Poster

Thanks, trying to improve now :)

Slavi 94 Master Poster Featured Poster

Kk thank you :)

Slavi 94 Master Poster Featured Poster

I c, but I'd really rather keep them in 1 file for this application :s Is there any way to refer to "this" when clicking login and the parameters of defaultHost and defaultPort to be refered to it? The problem with creating new GUI is on the new one i'd have to click login again which will create another gui and so on into an infinite loop .. Also the run() method seems to return exception cought pretty much every time when I run the program but it worked fine when I had it in the main, any ideas? :p Sorry to bother but i already tried everything I could think of myself.. :P

Slavi 94 Master Poster Featured Poster

Was hopping to avoid that James but if no other solutions I guess i'd have to :p

Slavi 94 Master Poster Featured Poster

would it even make any difference? They are stored in defaultServer and defaultPort variables, so I can just read them from those? Somehow I don't want to create new GUI when clicking log in, more like just connect with the current values on the already running one

Slavi 94 Master Poster Featured Poster

So, I've been making a gui for a client to connect to a server using it. What I want is when I press Login to connect with the specfied Server address and port name on the gui. If I have hardcoded the Server and port it works but I don't want it like that ... I want to be able to switch the server address and port on the go. Any ideas? I tried rearranging the code and what I get is everytime I click Login now it creates a new GUI. So basically I see the problem on this line
finalClient fc = new finalClient(defaultHost,defaultPort);
where I need the object of the class to be able to use run(); but ofc it creates new gui every time, and what I want to do is use the one created by default when lauching .. Here is the entire code:(omitting the implementation besides the GUI as its vector clocks n stuff)

class finalClient
{   
    private String sentence;
    private String modifiedSentence;
    private JFrame frame = new JFrame("Distributed Systems project");
    private JTextField textField = new JTextField(40);
    private JTextArea messageArea = new JTextArea(30, 40);
    private DataOutputStream outToServer;
    private PrintWriter out;
    private int defaultPort;
    private String defaultHost;
    private JTextField tfServer, tfPort;
    private JLabel label;
    private JButton login, logout;
    private Boolean connected;
    private int msgNumber;
    private Pattern msgre = Pattern.compile("\\[(.*?)\\] \\((.*)\\)"); //Regular expression for the vector clocks
    private Pattern vclock = Pattern.compile("\\((\\d+),(\\d+)\\)"); // regular expression for the numbers of the vector clocks
    private …
Slavi 94 Master Poster Featured Poster

change the output to string, compare if its equal to -1 if so replace with empty string

Slavi 94 Master Poster Featured Poster

1) because java has its own syntax that has to be followed upon object creation
2) because you can initialize your class i nthe constructor so whenever you create an object of that class, it will already have assigned parameters and be ready to use(for example GUI's)

Slavi 94 Master Poster Featured Poster

are you trying to multiply each element of the matrix with a corresponding element in the vector? as in element 1 in the matrix * element 1 in the vector?

Slavi 94 Master Poster Featured Poster

If you have to deep into inheritance and polymorphism perhaps bank account system is good to implement

Slavi 94 Master Poster Featured Poster

Your assignment states "Write a program in java " exercise 2, are you sure you want it in c++ ?

Slavi 94 Master Poster Featured Poster

What exactly is your task ...? Have a vector of numbers and sort them or?

Slavi 94 Master Poster Featured Poster
#include<iostream>
using namespace std;

void primes(int n){
  bool isPrime[n+1];
  for(int i=2; i<=n; i++)
    isPrime[i]=true;
  //
  for(int i=2; i<=n; i++){
    if(isPrime[i]){
      cout << i << " ";
      for(int j=2*i; j<=n; j+=i) isPrime[j]=false;
    }
  }
  cout << endl;
}


int main (){
  primes(100);
}

Congratulations your homework was done .. :D
iamthwee nice one :D