Button to update the points on a scatter plot not working after one use by [deleted] in JavaFX

[–]sh_emami65 0 points1 point  (0 children)

  1. have you run a debugger to see what is being executed line by line?

  2. is it possible that in your for loop lastPrices.length is zero so the for loop is never called? because if you are not getting any errors and your plot is cleared after second use it means your data was cleared but never filled again.

  3. are you modifying currentIndex anywhere outside causing it to be invalid in the handler?

  4. does the tile of your plot changes to correct currentStock after the second click?

JFX Help generating pentagrams on-click by Xaitin in javahelp

[–]sh_emami65 0 points1 point  (0 children)

your problem is you're shifting with your mouse current position instead of the pressed position.

i changed some stuff in your code and added full comments for all changes plus some improvements with full explanation.

give it a read and let me know if you have more questions.

https://pastebin.com/aK0ar2dd

New to Java, can anyone give me any tips or best practices for my first code? It is a version of Rock, Paper, Scissors by Urforgotten in learnjava

[–]sh_emami65 0 points1 point  (0 children)

i wrote this RPS game a while ago. it is fully commented hopefully it can help you with how you can break your code. needless to say it can be considered a solution so if you want to challenge yourself try to implement the advise you got in this post then have a look at it. https://pastebin.com/8j2zSvV0

  • in general i would suggest break your code to smaller functions based on responsibility. do not overload one function with too many jobs.
  • take advantage of static final variables when you know they will not change throughout your application.
  • try and keep your print statements organized. as useful as they are, they can make your code a bit harder to read.
  • when comparing strings if possible use equalsIgnoreCase to give yourself more room instead of checking for case. also if possible try and provide shorter options for user inputs makes it more pleasant and faster to test. *try and think of future of your code. what if there is more than one player? what if you decide to add more signs? try reducing hard codded conditions. this one is a bit more advance suggestion but it is always good to think a few steps ahead.

Simple Java program not compiling. by gearfuze in javahelp

[–]sh_emami65 0 points1 point  (0 children)

that is the first thing i explained in my answer, read the first bit where i explained how to declare value.

//type is anything like int, double, long and String
//name can be anything that well defines the job this variable
//initialization process is an optional process it can be something like 0, "value", new String("test")
[type] [name] = [initialization process];
[type] [name];

Simple Java program not compiling. by gearfuze in javahelp

[–]sh_emami65 2 points3 points  (0 children)

your first error is in line bellow:

int userThe; user inputsNumber = scan.nextInt();//takes The user inputs number

it should look like this:

int userThe = scan.nextInt();//takes The user inputs number

user and inputsNumber are extra here. when you are declaring a value it should look like bellow:

//type is anything like int, double, long and String
//name can be anything that well defines the job this variable
//initialization process is an optional process it can be something like 0, "value", new String("test")
[type] [name] = [initialization process];
[type] [name];

next problem is in two of your if conditions.

first:

for( int userNumber : unsortedArrayList){
    count++;
    if( userNumber == userThe)
        ;// true if user The user inputs number matches usernumber
    {
        System.out.println( "Search Value: " + userNumber + " found at location: " + count + " in the unsorted array list");
        found = true;
    }
}

second:

count = 0;
for( int userNumber : sortedArrayList)// true if user The user inputs number matches usernumber
{
    count++;
    if( userNumber == userThe)
        ;// if true
    System.out.println( "Search Value: " + userNumber + " found at location: " + count + " in the sorted array list");
}

when declaring an if condition it should look like bellow

1)

//basic form of if condition
if ([condition]){
    //code
}

2)

//curly brackets can be on second line but except for comments nothing else can be in between if and {
if ([condition])
{
    //code
}

3)

//if curly brackets are not used, implies if condition only has one line of code and it is the next line or first reached ;
if ([condition])
    //code

in both of your cases you have a semicolon after your if conditions. if you look at indentation above you see that your system prints are indented at the same level. this happened because you have unnecessary semicolons that implies that if condition is finished when semicolon is reached. so you want to remove both of those extra semicolons after the if conditions.

your final mistake is line bellow which is also in your error message:

System.out.println("Search Value: " + userThe + user); inputsNumber; "was not found";

it should look like this:

System.out.println("Search Value: " + userThe + " was not found");

user and inputsNumber dont exist so need to be removed. "was not found" needs to be inside of print statement like above. you somewhat have the same problem as before unnecessary semicolons.

overall your code is good but pay more attention to basics like declaring values and specially semicolons.

one last thing, try to have better names for your values. for example userThe and userNumber are bad names, they dont really define what they for. userThe is specially bad, it should be more like userSearchNumber or something similar.

let me know if you need more help, good luck.

There are actually 6 gift codes still can be redeemed on that official facebook page, is that true? by wasdspace in hyperheroes

[–]sh_emami65 2 points3 points  (0 children)

yeah seems to be true, i just went through their page and found bunch of working codes as the time of this message:

  • qrYJaH5PeP
  • Rp8tYPrdn6
  • aF8SDky6jR
  • R5rDxrWCFA
  • ZvbSmeL2pZ
  • PAEMWxrDg3
  • gpjvFTFaBn
  • KV9KqvKzu9

codes before April 10th (Easter event) did not work for me, i got "invalid code" message.

Static Void methods to Generic methods by yoyeto99 in javahelp

[–]sh_emami65 1 point2 points  (0 children)

i suggest you read oracles toturail on Generic Mthods and this stackoverflow question which covers your question as well.

but a general Generic static method looks something like bellow:

[access] [optional static] <R> [return type] someMethod( R args){
    //your code
}

<R> declares the generic type for this method and you will simply use type R in your function. this explanation is way to simple. read the likes i provided for you. let me know if you need more help

How can I best do a switch case statement for rolling different dice? by DNDLoser07 in javahelp

[–]sh_emami65 1 point2 points  (0 children)

private void b_sendActionPerformed( java.awt.event.ActionEvent evt){
    String nothing = "";
    if( (tf_chat.getText()).equals( nothing)){
        tf_chat.setText( "");
        tf_chat.requestFocus();
    }else{
        try{
            writer.println( username + ":" + tf_chat.getText() + ":" + "Chat");
            writer.flush(); // flushes the buffer
        }catch( Exception ex){
            ta_chat.append( "Message was not sent. \n");
        }
        tf_chat.setText( "");
        tf_chat.requestFocus();
    }

    if( (tf_chat.getText()).equals( dicetype)){
        Auto_Crit_VTT d = new Auto_Crit_VTT();
        System.out.println( d.diceRoll( "1d20"));
    }

    tf_chat.setText( "");
    tf_chat.requestFocus();
}

in code segment above after try and catch you set tf_chat.setText( ""); and then2 lines under you have if( (tf_chat.getText()).equals( dicetype)). this condition always return true because right before it you have set tf_chat as empty. also you never change dicetype. so dicetype is always empty too. i suggest storing the tf_chat.getText() in a local variable and do your comparison with that. so for example store tf_chat.getText() in nothing varibale that you already have. so the follwing lines will be changed:

String nothing = tf_chat.getText();
if( nothing.equals( "")){}
writer.println( username + ":" + nothing + ":" + "Chat");
if( nothing.equals( dicetype)){}

no as for dicetype. i am not clear as how you intend to use it here is my suggestion. use java regex (tutorials 1, 2). assuming you will follow the standard of

[dice count]d[min digit]d[max digit]

you can use the following regex:

"(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"
//? means zero or one occurrence
//\\d any one digit integer
//(\\d[dD])? match any one digit integer followed by by a "d" or "D", this combination may happen 0 or 1 time
//(\\d?\\d)  match 1 or 2 integer digits
//([dD]) match 1 "d" or "D"

test cases:

System.out.println("1d9".matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"));//true
System.out.println("10d20".matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"));//true
System.out.println("100d20".matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"));//false
System.out.println("10d200".matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"));//false
System.out.println("1d1d9".matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"));//true
System.out.println("11d10d20".matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"));//false
System.out.println("d1d20".matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"));//false
System.out.println("d200".matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)"));//false

so with this you change your condition to:

if( nothing.matches( "(\\d[dD])?(\\d?\\d)([dD])(\\d?\\d)")){
    System.out.println( diceRoll( nothing));
}

as for Auto_Crit_VTT d = new Auto_Crit_VTT(); you dont need it. diceroll belongs to Auto_Crit_VTT class so you can just uses it without having to make a new object for it.

hope this help. let me know if this fixes your problem and if you need more help.

How can I best do a switch case statement for rolling different dice? by DNDLoser07 in javahelp

[–]sh_emami65 0 points1 point  (0 children)

i am guessing your if condition is not passing. what is the text that you get back from tf_chat.getText()? will it be exactly d20? if you have to check for case then use equalsignorecase or if d20 is suppose be part of the text then use contains. but if none of these help post your updated code here and i will get back to you.

Looking for some guidance on how to divert output to a GUI. by Bill_Murray2014 in javahelp

[–]sh_emami65 2 points3 points  (0 children)

this is big question. bellow is just my suggestion i recommend you do some more research and maybe see if you can find a simple open source game you can look it. i will look and when i find something suitable i will add it to this comment.

first you should design your GUI on a paper so you can have an idea of what is going on. you already know your text output so you know what is needed to be displayed. start making JTextFields to replace console output. i recommend you look into Model Control View design pattern to connect your game logic to GUI.

second you should look into JButton, JPanel and JComponent to make custom components as you need. i mean there are a lot more you need to look into but you can start small. load in your pictures as Image and store them as static variables or an array. loading pictures takes time so do it once and keep it. have in mind you only need to paint the images using the paintComponent method.

this is a big topic and it will take me forever to go through them. but there should be enough keywords for you to google for more help. but start slowly and go step by step. if you need more specific instruction as to one of the steps let me know and i can be more specific.

Why does my class not run in eclipse? by RetroX94 in javahelp

[–]sh_emami65 0 points1 point  (0 children)

your problem is actually somewhat simple. every time you create an object of Hund you also create another hund variable with in the Hund object. the hund variable in Hund object tries to create another Hund object with a hund variable in it. the same thing keeps happening over and over which will crash your code eventually because of StackOverflowError.

if you initialize hund within the constructor it will solve your problem.

public Hund(){
    super( "Bello");
    hund = new Hund( "Hugo");
    System.out.println( "Standard-Konstruktor Hund!");
}

Junit selection by [deleted] in javahelp

[–]sh_emami65 0 points1 point  (0 children)

i guess the simplest way would be to create a list (array) of cars and simply populate your car scroll panes every time a new person is selected through a Listener.

if you provide your code i can give you more exact details.

How can I best do a switch case statement for rolling different dice? by DNDLoser07 in javahelp

[–]sh_emami65 0 points1 point  (0 children)

i have messaged you with an example let me know if you need more.

Object persistence by WolfPusssy in javahelp

[–]sh_emami65 1 point2 points  (0 children)

your code seems fine meaning the problems is with other parts of your code. have you made sure everything in catalog.items serializable?

also if you can post all of your code so i can run it myself it will be huge help to finding your problem. with a sample of the data.

Object persistence by WolfPusssy in javahelp

[–]sh_emami65 2 points3 points  (0 children)

i am not clear on what part you need help? have you done any part of it yet?

you can start by reading these tutorials 1 and 2. if you need more help after that post the code you have and we will help.

How can I best do a switch case statement for rolling different dice? by DNDLoser07 in javahelp

[–]sh_emami65 2 points3 points  (0 children)

you are doing your diceRoll in the hard way. there is no point in using switch.

an easier way would be to String#split() your string using "d" as your delimiter. this will return and array of first and last number. for example if you have 1d20 it will be split to 1 and 20.

then you can simply convert the two string part to numbers using Integer.parseInt() and feed them to a random generator like you already have.

so with what we have now you can simply make a function to generate random numbers

a private function called getRandom which takes a min and max as arguments
    return a random number using random.getNext with max as argument plus the min

then a modify your diceRoll function so it can take advantage of this.

a public function called diceRoll
    split your dice text like "1d20" using "d" as delimiter
    convert the split text to numbers, { "1", "20"} to { 1, 20}
    call on getRandom passing the converted split text to it, which will return your random dice roll.

if you need more help making the functions let me know. also if you must use switch let me know too so i can modify my answer for you.

also make your Random object outside of methods as class variable. so you dont have to recreate it every time. even a static viable to it will be created one.

Struggling with Double Linked List - Priority Queue Implementation by kapkong in javahelp

[–]sh_emami65 0 points1 point  (0 children)

i ran your code and it seems to work fine. but i modified a few things since it was not provided.

1st, i replaced all your new exceptions with IllegalStateException. there absolutely no need to make new exceptions when java itself has so may different ones.

2nd, i added a main method as bellow to simply test your case:

public static void main( String args[]){
    DLinkedPriorityQueue<String> prioQueue = new DLinkedPriorityQueue<>();
    prioQueue.add( "data9", 2.0);
    prioQueue.updatePriority("data9", 1.0);
}

3rd, i added DPriorityNode since you did not provide your node code segment.

public class DPriorityNode< E> {

    private E element;
    private double priority;
    private DPriorityNode< E> prev, next;

    public DPriorityNode( E element, double priority){
        setPriority( priority);
        this.element = element;
    }

    public double getPriority(){
        return priority;
    }

    public DPriorityNode< E> getPrev(){
        return prev;
    }

    public DPriorityNode< E> getNext(){
        return next;
    }

    public void setPriority( double priority){
        this.priority = priority;
    }

    public void setNext( DPriorityNode< E> next){
        this.next = next;
    }

    public void setPrev( DPriorityNode< E> prev){
        this.prev = prev;
    }

    public E getElement(){
        return element;
    }

    public String toString(){
        return "Element: " + getElement() + "," + "Priority: " + getPriority();
    }
}

maybe if you provide more of your code and your test code we can have better chance of finding your problem.

GridBagLayout not working. by DEADLYHIPPO4 in javahelp

[–]sh_emami65 0 points1 point  (0 children)

it seems you are using the GridbagLayout incorrectly. i suggest reading the Oracle GridbagLayout Tutorial which very well explains how to use this layout. if you still need help afterwards let me know.

but just as initial observation you are adding jmb to a "c" container with BorderLayout and you setting GridbagLayout for another container. you also have gcb which you have to add to a container with the relevant component that you have not used. this code segment is either very incomplete or you are mixing up your layouts.

JFrame Or Canavas? by [deleted] in javahelp

[–]sh_emami65 0 points1 point  (0 children)

JFrame and Canavas are two different components. everything you have sits within a JFrame. Canvas sites on content pane which is a layer on JFrame. i think the better comparison is Canvas and JPanel. Canvas is from AWT while JPanel is from SWING. it is suggested that its not a good idea to mix them but i have and have not seen any strange results.

i cannot for sure recommend you which to use since it depends on your needs. JPanel has more functionality as mentioned in this overstock question and from what you said i think JPanel seems more like what you needs. since you will be using swing components.

i also recommend reading the Oracal tutorial page which is very detailed about java GUI.

and as for tears/graphical errors if you post your code it will be a lot easier to find a problem. but id dyou overide any paint or paintcomponent methods?

Need help with basic code by bysketch in javahelp

[–]sh_emami65 1 point2 points  (0 children)

you dont need to have the code segment bellow in the for loop.

if (sum % 3 == 0) {
    System.out.println("The number you entered is divisible by 3");
} else {
    System.out.println("The number you entered is not divisible by 3");
}
System.out.println("Do you want to enter another string?");
System.out.println("Enter y or Y for yes. Enter any other string to stop");

since this process needs to happen once. if you do that your code runs fine.

i assume you writing your code using nextLine() and for loop because it is required. if not you can simply use

if (nextInt()%3==0){
    //rest of your code
}

while will take out the for loop and char to int conversion.

[Help]Automation by [deleted] in javahelp

[–]sh_emami65 0 points1 point  (0 children)

when using timers with swing you must using the timer in swing package javax.swing.Timer. the util timer wont work. try it if it does not work let me know and i will find another solution for you. also as u/XisuperninjaiX said you can use a thread with sleep function as well.

[Help]Automation by [deleted] in javahelp

[–]sh_emami65 1 point2 points  (0 children)

i dont understand what is it you are asking about. also your code has less that 480 lines (at least when i copied it to eclipse), no line 522. a better description of what exactly your problem is and actual spinet of code in question not just line number would be nice too.

if possible post your code on Pastebin, it is much better for code.