Dividing by four in combobox - java

I'm a new programmer doing a date of birth selector for part of my project. I have got everything set up apart from a few things and I am unsure of how to do these things.
ArrayList<String> years_tmp = new ArrayList<String>();
years_tmp.add("Year");
for(int years = Calendar.getInstance().get(Calendar.YEAR) ; years>=Calendar.getInstance().get(Calendar.YEAR)-90;years--)
{
years_tmp.add(years+"");
}
Y = new JComboBox(years_tmp.toArray());
Above is my part of my code for a JComboBox which lists the previous 90 years and has the word "Years" as the first object.
For my code above how would I list the years like it currently does, but to only display years which divide by four exactly (leap years)?
Also how do I make it so once the JComboBox list has been opened the selection years can not be selected so when the value is saved in my save file it does not allow the save of the word "Years"?

To get a value which can be devided be 4 without a rest you can use the modulo operator '%':
if(year % 4 == 0) {
...
}
To disallow the Selection of "year" itself, you have several ways. One could be to append an ItemListener to your ComboBox and check whether the user selected the "year" value. If the user selected this value you can print an error message or just select another value - maybe the next possible one. You also can do more fancy stuff like disabling the save Button if the user selected an invalid value..
I think this should help you to get to the right direction.

Related

Guess the image game, Random Image generator without repetition [duplicate]

This question already has answers here:
How to generate random numbers without repetition
(5 answers)
Closed 1 year ago.
I wanna make image guessing android game, there are 150 images, I want the image to be generated randomly, and the user should type the name of character if it is correct the next button will display another image till the 150 are finished
This the code for avoiding image repition
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.myImage);
int position = new Random().nextInt(list.size());
imageViewObject.setImageResource(Integer.intValue(list.get(position)));
list.remove(position);
but how should I compare the user input with character name, should I use image path, or make array for answers and compare it to user input?
There are many ways to do it, depending on your project. An effective one would be storing them in an object (let's call it Card), and card will contain as properties (imageURL as String / characterName as String/ ifCorrect as Boolean).
Store these objects inside an array and if the user guesses the right answer, change the object's ifCorrect field to true, so you know the image has been played.

How to verify the user input in a textfield to compare it with a variable?

I want to verify the user input (number) in a textfield that if it's bigger than 9 or not. Note that the number from 1 to 9.
if it's bigger than 9 I want to show a jOptionPane
some code i have traied:
else
if(jTextField1.contains()){ // want to compare it if it's bigger than 9 or not
jOptionPane1.showMessageDialog(this,"Please enter the number of the tab"); // wich means from 1 to 9
}
So how to do that with java?
Thanks in advance :)
You can directly do it with help of js using jQuery just give id to our text field and add a script tag with the following code :
num = $("#yourId").val();
if(num>9){
alert("your message");
}
or any thing else you want to achieve

Can't access my array

My programme is to create a Train Route Finder using Java, first off as a command line programme then convert into a GUI. That's the least of my worries for now. I am currently stuck on one functionality of my system and that is to display the list of stops between two stations that the user previously input.
Now, I have been able to prompt the user to choose their stops (lines 3,149 - 3,171 in my code):
if(deptChoice == 1 && arrChoice == 2){
List<String> stopList1_2 = new ArrayList<String>();
Scanner stopPick = new Scanner(System.in);
stopPick.useDelimiter(System.getProperty("line.separator"));
do {
System.out.println("\nCurrent list of stops between Leicester to Loughborough is:\n\n" + stopList1_2);
System.out.println("\nWould you like to add a new stop? (Please enter 'Yes' or 'No')\n");
if (stopPick.next().startsWith("Yes")) {
System.out.println("\nPlease type in the stop you wish to add to the route:\n");
stopList1_2.add(stopPick.next());
}
else {
break;
}
} while (true);
String[] stopArr1_2 = stopList1_2.toArray(new String[0]);
System.out.println("\nCurrent stops between Leicester to Loughborough is:\n\n" + Arrays.toString(stopArr1_2));
}
and the stops they type in are added to an array. When they are satisfied with the amount stops between the stations there are then the loop ends and it displays the array of stops between Station X and Station Y.
However, here comes the problem:
I want to then gain access to this previously created array with all the exact stops further up my code. In this "if" statement, if it is satisfied, then I want the array (stopArr1_2) to be displayed (in lines 3,019 - 3,021):
if(deptChoice == 1 && arrChoice == 2){
System.out.println(""); //this should be where I call the array back to display itself
}
as those stops would correspond with the users choice of deptChoice == 1 (Station 1 = Leicester) to arrChoice == 2 (Station 2 = Loughborough).
I hope this is clear:
I basically want the array of stops inputted by the user to reappear when they choose Train Routes.
Here his my full code I made in Notepad++ I thought it would be better for you lot to see all of my code rather than small segments and it is commented.
Furthermore, to comprehend my problem run my programme. To understand my problem do this:
1) Compile Train.java (javac Train.java)
2) Run programme (java Train)
3) Pick Admin Menu (Number 5)
4) Pick Input Menu (Number 1)
5) Pick any two stations
6) Enter a couple of stops until you're happy
7) When it shows the final stops array for those two stations the programme seems to end so hopefully that is not an issue
8) Run programme again (java Train)
9) Pick Train Routes (Number 3)
10) Pick the SAME stations in the same order as you did before so it would find the array you just made
11) Now nothing will come up because, well, that's my problem. I don't know how to regain that array I just made for it.
Download link to my Train.java file (and Train.txt file if needed):
https://www.dropbox.com/s/7dy0pp9vyykwhrk/TrainJava.rar
Any suggestions?
Whats the location of your second block of code ? ( The code where you said you need access to the List<String> stopList1_2 ) ?
1 If its in the same class but in a different method , then
Move this declaration
List<String> stopList1_2 = new ArrayList<String>(); ( currently in your first block of code ) to a class field.
BUT
2 If its in the same class and also in the same method as your first block of code, then
Move this declaration
List<String> stopList1_2 = new ArrayList<String>(); ( currently within the if statement of code ) to outside the if condition .
but treat this as a quick recipe for you problem at hand. More importantly - please read the variable scoping rule and access specifiers in java.

Choices for combobox in a repeat

I've got a view that contains documents with various questions I want answered about Purchase Orders.
Using a repeat, I list all the questions. There are a few different kinds of questions, so I only render the answer field that I need based on the FieldType column value. I want to pull the choices for a combobox from the DialogChoices field on the question document.
I'm currently getting the choices showing as plain text on the next line after the empty combobox instead of as the selectItems. Where is my code going wrong?
<xp:comboBox id="comboBox1">
<xp:this.rendered><![CDATA[#{javascript:rowData.getColumnValue("FieldType") == "Dialog Box"; }]]></xp:this.rendered>
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:var doc:NotesDocument = rowData.getDocument();
var choicesVector:java.util.Vector= doc.getItemValue("DialogChoices");
var choices = [];
// loop through the vector, doing push into the array
for (i=0; i<choicesVector.size(); i++) {
choices.push (choicesVector.elementAt(i))
};
return choices;}]]>
</xp:this.value>
</xp:selectItems>
</xp:comboBox>
Strange, but a test database with the code above does not seem to give me strange results. Maybe it is because the data is in fact not an Vector but just a string?
Here are some tips :
The first thing you could change in your code is the loop to get all the data out of your field. Since the value property of a combobox already expects an array or vector you can change the code to something like:
<xp:this.value><![CDATA[#{javascript:var doc:NotesDocument = rowData.getDocument();
return doc.getItemValue("DialogChoices");
}]]>
</xp:this.value>
But it would be even better to remove the getDocument call at all. If possible you can add a column to the view are you are using for the repeat's datasource. In this column you get the data from the field directory. This way you can use the viewentry's getColumnValue() which is a performance optimization. Something like:
<xp:selectItems>
<xp:this.value><![CDATA[#{try{
return rowData.getColumnValue("DialogChoices");
}catch(e){// do something }]]>
</xp:this.value>
</xp:selectItems>

I dont know how to make dropdown in java

(using netbeans and java)
I have the following
1 text field named input 1 (named x5)
1 text field named input 2 (named plus10)
1 text field named input 3 (named plus5perc)
1 answer field (an uneditable text field)
1 button
When a number is placed into either input a calculation is done when the calculate button is pressed e.g. if i put in 2 in input 1 and click the button = input1 * 5 and the answer is displayed in the answer field
when 2 is put into input 2 = (input 2 + 10) * 5
when 2 is put into input 3 = input 3 + 5%
instead of having 3 input fields i would like 1 drop down list and one input
so you choose from the drop down which you want and only have 1 input field.
i don't know how to do dropdowns etc and any help would be appreciated
edit
anyone know how to on load hide the 3 inputs and then show the relivant input once it is selected from the combo box?
The drop down is called combo box in most UIs. The Java swing object is JComboBox
Here's the doc:
http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html
And a tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
I gave this a try (hope that's what you want).
With all that links and tutorials already provided, you should have been able to do that (IMO).
That's what it looks like:
Screenshot http://img97.imageshack.us/img97/9557/socombobox.png
It does not do proper exception handling, does not round the results and is not really object oriented (just uses hardcoded indexes, be careful when changing).
Add the components (called txtInput, cmbChoose, btnDo and txtResult in my case.
Edit the model property of your JComboBox, using Combo Box Model Editor and set it to
x5
plus10
plus5perc
This will generate the following source:
cmbChoose.setModel(new javax.swing.DefaultComboBoxModel(
new String[] { "x5", "plus10", "plus5perc" }));
Put the following into your JButtons ActionPerformed method.
try {
float input = Float.valueOf(txtInput.getText());
float output = 0;
switch (cmbChoose.getSelectedIndex()) {
case 0:
output = input * 5; break;
case 1:
output = input + 10; break;
case 2:
output = input * 1.05f;
}
txtResult.setText(String.valueOf(output));
} catch (Exception e) {
txtResult.setText("[Error]");
}
Sorry about the confusion.
please ignore the other post.
answer from user: italy
two approaches:
(1) Use setVisible - When you create the fields invoke setVisible(false) on each. When a selection is made in the combo box invoke setVisible(true) on the relevant input field and setVisible(false) on the others.
(2) Use one input field - when a selection is made on the combo-box change its name

Categories

Resources