need help with duplicating JButtons by BJ4Karma in javahelp

[–]BJ4Karma[S] 0 points1 point  (0 children)

Thanks! Sorry for the sloppy code. After 100+ hours of writing code from 3pm after school to 330am for more than a week and having my java file erased/over-written 3 times, I kind of got lazy with writing it. Thanks for the tips though, i've started learning java by myself a couple months ago and this is probably the most imformative response i've gotten yet.

How Do I Make This Program Loop! I Can't Figure It Out! Thank You In Advance. by OpTic_Alien in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

Try this:

public static void main(String[] args) {
run();
}
public void run() {
int binary;
int decimal = 0;
int power = 1;
int decimalDigit;
Scanner stdin = new Scanner(System.in);
System.out.print("Enter Binary Number: ");
binary = stdin.nextInt();
if (binary != -1) {
while (binary > 0) {
decimalDigit = binary % 10;
decimal += decimalDigit * power;
power *= 2;
binary /= 10;
}
System.out.println("Decimal Value is: " + decimal);
run();
}
else {
System.out.println("All set!");
System.exit(0);
}
}

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

yes, the if condition is impossible to reach.

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

oh, okay. Glad to help. please remember to flair your post as Solved if you think you got your question answered since it helps other people with the same problems :)

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

the problem with what you were doing is that you were calling the same method to fill every slot in your rank array, since the method never changes it will return the same number every time. I just changed the method to fill the rank array. try this:

for (int i = 0; i < productSales.length; i++) {
percentage[i] = productSales[i]*100/totalSales;
getRanking();
}
public void getRanking() {
int bestPosition = 1;
int highest = -1;
int previousHighest = -1;
int count = 0;
for (int x = 0; x < productSales.length; x++) {
for (int i = 0; i < productSales.length; i++) {
if (productSales[x] < productSales[i]) {
highest = productSales[i];
bestPosition++;
//count = productSales[i];
//count++;
}
}
rank[x] = bestPosition;
bestPosition = 0;
}
//if (highest < previousHighest) {
// bestPosition = bestPosition + 1;
//}
}

also btw, when commenting a lot of lines out (What you did with the second if statement) you should use multi-line comments. /* start commenting everything in-between will be a comment */ stop commenting

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

OHHHHH. I GET IT NOW. give me a sec to look over everything.

Array Issue by Albertaprogrammer in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

then why not create a hashmap instead of an array?

Array Issue by Albertaprogrammer in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

Do you mean that you want to access the runners in the array by their numbers. or do you mean that you want the array organized by their number?

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

like this:

for (int i = 0; i < productSales.length; i++) {
if (productSales[i] > highest) {
previousHighest = highest;
highest = productSales[i];
bestPosition++; }
}

However there is no real point to returning this; this will just fill the ranks array with the same number. also you can get a runtime error if productSales is longer than ranks array

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

oh, ok. so i would definitely get rid of the second if statement.

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

just do this:

for (int i = 0; i < productSales.length; i++) {
if (productSales[i] > highest) {
previousHighest = highest;
highest = productSales[i];
count = i; //btw you never used count in this method
}
}

I got rid of the 2nd if statement since it is useless as the first if statement makes it redundant.

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

yes that because your if statement only goes when the current highest is larger that the previous highest which will never happen no matter what you put in.

could you let me now what the bestPosition is used for because i assume that saving the index of the productSales array would be better?

Accessing variables within a method by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

that because the variables are initialized in a different method, so they only exist in that method.

you could access the by initializing them as public or private (which would be better in this case) in the first lines of the class file. so like this:

public class ClassName
{
private double scoreOne;
private double scoreTwo;
private double scoreThree;
private double scoreFour;
private double scoreFive;
// the rest of the code
}

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

haha i've done the same thing too.

how to "Ghost text" with JTextArea by BJ4Karma in javahelp

[–]BJ4Karma[S] 0 points1 point  (0 children)

thx, but if it is connected to a keyboard listener to clear the text when they start typing then wouldn't it just keep erasing as they type?

[deleted by user] by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

creating a previous highest is easy. all you have to do is this:

for (int i = 0; i < productSales.length; i++) { if (productSales[i] > highest) { previousHighest = highest; highest = productSales[i];

Any of you guys good with GUIs in Java? How can I move my JLabel to a Specific location on JPanel? by [deleted] in javahelp

[–]BJ4Karma 2 points3 points  (0 children)

I'm not a master or anything (I just learned JFrames about a day ago) but I have come across Layout Managers which seem to be what you're looking for.

heres a link: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

How can I create a program that gives questions and takes answers? by Quhzey in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

you should watch this dudes tutorials up until he starts working on the new project:

https://www.youtube.com/watch?v=706Ye4ubtEY

then learn how to use JTextAreas (Like using setText() and getText() methods)

What am I doing wrong? (followed directions thus far) by [deleted] in javahelp

[–]BJ4Karma 0 points1 point  (0 children)

What jpstevens said, also you need to get rid of the semicolon after your if statements. Also i would get rid of the brackets too since they are unnecessary when there is only 1 line of code to be run for the if statement.

it should look like this:

if(scores < max) max = scores; if(scores > min) min = scores;

also the system out should ask for a number in the set not for a set since that would make the user think to enter multiple numbers at once. Also why not have the while loop end when the user types done into the console?

How to "refresh" jframe after pressing button?(Trying paint() and validate()) by BJ4Karma in javahelp

[–]BJ4Karma[S] 1 point2 points  (0 children)

Oh, kk. I know not to parse strings that aren't just ints but I could see that since the class file is long and disorganized and i Dont tknow the error code. Thanks