Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
So I'm making a simple game in java. In this game I want there to be an infinite number of players.
The game is Dictionary, but not the one where you race to find a word in a dictionary.
How to play:
A person chooses a word that no one would know.
Everyone enters a made up definition that is believable.
Each person guesses which one is the real definition.
You get a point for guessing correctly and for the number of people who guess your word
(Steps 2 and 3 are ignored by the person who chooses a word)
My program (So far):
Choose start or quit
When start is pressed the number of players selected (Not including word selector).
Word selector chooses word in JPasswordField
Enters definition in JPasswordField
The word is told to Player 1 and then they must enter a definition into a JPasswordField
Repeats step 5 until the times repeated are equal to the number of players.
The problem I'm having:
Player + numberrepeated = passwordfield.getText;
doesnt exist.
I need to have the variable change each time.
You can't have dynamic variable names in Java. What you can do is use a Map, to associate a key (the player's name, for example), with a value (the player's password, for example):
Map<String, String> passwordsByPlayer = new HashMap<>();
...
passwordsByPlayer.put(playerName, passwordField.getText());
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to code a voting system, with candidate names and input.
Each voter will be given a screen with JTextfields to score each candidate, each score goiing into an array where i is the candidate number and score[i] is the candidate score.
I am having trouble sorting the arrays from highest score to lowest whilst being able to see which candidate has which score and then being able to use that to display the winner.
I have seen bubble sorting but that does not always sort it exactly from high to low and i've also seen the Arrays.sort(ArrayName); method.
Can anyone help?
It sounds like you're using parallel arrays, and if so, stop. Instead move away from the GUI program for a minute and instead create some decent OOP compliant classes, including a class for Candidate which holds the score and candidateNumber data. Then it would be easy to sort your collection (such as an ArrayList<Candidate>) using Collections.sort(...). If your Candidate class implements the Comparable<Candidate> interface, then your job gets even easier still since no Comparitor class would be needed. Note that if you have the class implement Comparable, you'll need to give it a public int compareTo(Candidate o) method that will return an int, 1, 0, or -1, depending on the score of the o Candidate vs. this Candidate's score.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
hi i have a java programming assignment wich include 3 exercice i have done 2 already but in the third one i am dont know wich data type i should use
here is the question :
Write a java program that reads from the user the course code, section and the scores of a student for three exams representing two midterms and a final. The percentage of each of the three exams is of fixed value 30, 30 and 40 percent respectively. Your program computes the final average grade and displays it along with the course code and the section.
Remark: All data, except for the average, must be whole numbers and you should use the most efficient data type that is suitable for this specific exercise.
Sample Run:
Enter your course code: CSCI250
Enter your section: E
Enter the scores of the tests and the final: 97 83 77
CSCI250 E 84.8 (result)
so what i want to know is what is the preferable data type to use for course code ? and char is the one that i should use for section right ?
If you're capturing user input, use a String for everything.
Reason? You may request a number, but the user can type anything. Your code needs to handle bad input.
I think you can use String as data type for Course Code. You can write both numbers and letters by using it. And for section, yes, char will be suitable for it.
Use a String for arbitrary text.
If the section code is always present1 and is never more than character than a char can be used.
However, I would still use a String for consistency, flexibility, and easy of use. The teacher may prefer this based on the "efficient"cy they are going for.
1The is a soft "always": while char cannot represent null a sentinel (eg. '\0' or ' ') can be used to indicate 'no section specified'. Using such a sentinel to supplement null can also (but does not always) lead to more logic work - in particular when the record is displayed.
In any case, it is probably best to not switch to Character just for the null as this is most likely outside of the scope of current course work.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I try solve some problem in the arena, but now I can not find it there. I just have a text of solution:
DEFINITION
Class Name: MatchMaker
Method Name: getBestMatches
Paramaters: String[], String, int
Returns: String[]
Method signature (be sure your method is public):
String[] getBestMatches(String[] members, String currentUser, int sf);
PROBLEM STATEMENT
A new online match making company needs some software to help find the “perfect
couples”. People who sign up answer a series of multiple-choice questions.
Then, when a member makes a “Get Best Mates” request, the software returns a
list of users whose gender matches the requested gender and whose answers to
the questions were equal to or greater than a similarity factor when compared
to the user’s answers.
Implement a class MatchMaker, which contains a method getBestMatches. The
method takes as parameters a String[] members, String currentUser, and an
int sf. Here members contains information about all the members. Elements of members are of the form
NAME G D X X X X X X X X X X
NAME represents the member’s name
G represents the gender of the current user.
D represents the requested gender of the potential mate.
Each X indicates the member’s answer to one of the multiple-choice
questions. The first X is the answer to the first question, the second is the
answer to the second question, et cetera.
currentUser is the name of the user who made the “Get Best Mates” request.
sf is an integer representing the similarity factor.
Can you help me, and tell how to find a solution in the TopCoder arena?
use the problem archive to find out which SRM or tournament the class was used in.
http://community.topcoder.com/tc?module=ProblemArchive
this will show you the top submissions from each of the acceptable programming languages. You can also use the summary button if you know which SRM the class is from but this does not guarantee that the solution you view is correct if the problem was an old one.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
When I click on button the result display as Nan. And if I provide value of T directly for Example T=15 than 15 will be print out.
This is Formula.
T = (P*R)/ ((S*E)-(0.6)*P)
Where P , R , S is variable from Text Box.
And E is variable from Drop Down Box.
From my perspective, T is going to be a decimal, so the first thing you should look into is how to cast from a Java String to a double, using the Double.parseDouble() method, which you can read about here.
If you don't know how to grab the value from the text box itself, then you should read about JTextField, and its methods here. Finally, if you don't know how to get a value from a JComboBox, then you can read this tutorial.
Once you've worked out how to get the value from the text box or drop down list and parse it into the correct data type, you can get onto performing the calculation.
double t = (p * r)/((s * e) - (0.6) * p);
You'll also notice I've given you very little code. This is because you've given me nothing to work with. Happy reading!
Check your input for S & E, if it is 0 (zero) then your output always show NaN what stands for 'Not A Number'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am doing an assignment and I have to do several simple string manipulations. I think I have the last one figured out, and it works by itself, but it does not work when I put it together with the other string manipulations, giving me an arrayoutofbounds exception error. Any advice?
Here is the small code I made
public static void main (Strings[] args) {
Scanner sc = new Scanner(System.in);
String theSentence = sc.nextLine();
String [] theWords = theSentence.split(" ");
Arrays.sort(theWords);
System.out.println(theWords[1]);
System.out.println(Arrays.toString(theWords));
}
This does not work when it is put together with the rest of the code even though it works by itself. For reference, this code is supposed to take in a small sentence and give me the smallest word lexicographically. Ex: input: "4 WHAT WAIT IS THIS" output would be "IS"
The code is assuming that theWords will always have at least two elements in it. If the sentence provided by the user does not have any spaces, theWords will never get an element in position 1 and so the program will crash on System.out.println(theWords[1]);
In Java arrays are indexed from zero, where the 0th element is the first.
As Mark said you are assuming that there are always two words entered in this case. You should check the length of theWords before trying to access an element.