I have a simple JForm that has some text fields.The problem is, my evaluate() method is not cycling through the text fields as I expected.All I get is the first text fields, of getText() that are executed. I'm stuck in this. Help would be great. Here is the code. Also here is a screen capture of the program: https://www.dropbox.com/s/w5ie4dfnc9wi216/Capture.JPG?dl=0
public MAppGest() {
initComponents();
}
public double a=2.6;
public double index0;
public Iterator itr;
public void getText(String ind, String inde){
evaluate();
index0=Double.parseDouble(txtNewIndex.getText())-Double.parseDouble(txtOldIndex.getText());
txtTotalIndex.setText(Double.toString(index0));
txtRoom1.setText(Double.toString(index0));
txtTotal1.setText(Double.toString((index0*a)));
}
public void evaluate(){
if(Boolean.valueOf(txtNewIndex.getText())&&Boolean.valueOf(txtOldIndex.getText())==true){
getArray();
}
else if(Boolean.valueOf(txtNewIndex2.getText())&&Boolean.valueOf(txtOldIndex2.getText())==true){
getArray();
}
else if(Boolean.valueOf(txtNewIndex3.getText())&&Boolean.valueOf(txtOldIndex3.getText())==true){
getArray();
}
else if(Boolean.valueOf(txtOldIndex4.getText())&&Boolean.valueOf(txOldIndex4.getText())==true){
getArray();
}
else if(Boolean.valueOf(txtNewIndex5.getText())&&Boolean.valueOf(txtOldIndex5.getTex())==true){
getArray();
}
else{
JOptionPane.showMessageDialog(MAppGest.this,"An error occured");
}
}
public void getArray() {
ArrayList<String> al=new ArrayList<>();
al.add(txtNewIndex.getText());
al.add(txtOldIndex.getText());
al.add(txtNewIndex2.getText());
al.add(txtOldIndex2.getText());
al.add(txtNewIndex3.getText());
al.add(txtOldIndex3.getText());
al.add(txtOldIndex4.getText());
al.add(txOldIndex4.getText());
al.add(txtNewIndex5.getText());
al.add(txtOldIndex5.getText());
for (int i = 0; i< al.size(); i++) {
String fields []=null;
fields[i] = al.get(i);
}
}
private void btnGrandTotalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getText(txtNewIndex.getText(),txtOldIndex.getText());
getText(txtNewIndex2.getText(),txtOldIndex2.getText());
getText(txtNewIndex3.getText(),txtOldIndex3.getText());
getText(txtOldIndex4.getText(),txOldIndex4.getText());
getText(txtNewIndex5.getText(),txtOldIndex5.getText());
}
If your problem is that evaluate() is only completing the first if statement, it's because you are using if-else statements when you want to just use if. If you use if-else, once the first if is verified true it will skip the rest. If my answer is not what you are looking for, then I advise you to ask a better question. Like the comments have said, give an SSCCE so the answerers aren't left guessing.
A side note: Boolean.valueOf() returns a boolean, so your ==true is unnecessary.
Related
I have a method splitThat which does the following:
public static void splitThat(String newString){
System.out.println
(java.util.Arrays.toString(newString.split("\\s+")));
}
The method split returns a string array, but I do not know how to reach elements of the returned string array seperately. Example:
splitThat("Please help me");
This will return the array:
[Please, help, me]
When I want to reach to the first element of the array, I can't simply use index, the compiler gives error messages and suggested fixes do not help. How I can do this?
This will return the array:
The void method you've written doesn't return anything. It only prints the array.
You might want to change your method to something like:
public static String[] splitThat(String newString){
return newString.split("\\s+");
}
Your splitThat doesn't return anything. It just prints the array returned by the call to split. Change your method to return a String[] instead of printing the array if you want to access elements of that array.
public static String[] splitThat(String newString){
return newString.split("\\s+");
}
...
String[] s = splitThat("Please help me");
if (s.length > 0)
System.out.println(s[0]);
This Is Copy As You Want
public class TwoD {
public static String[] splitThat(String newString){
System.out.println(java.util.Arrays.toString(newString.split("\\s+")));
String []temp = newString.split("\\s+");
return temp;
}
public static void main(String... args)
{
//Use This
for(String outputs:splitThat("Please help me")) {
System.out.println(outputs);
}
//OR USE
String []outputs2 = splitThat("Please help me");
for(int i = 0;i < outputs2.length;i++)
{
System.out.println(outputs2[i]);
}
}
}
I've tried creating a public static variable (NOT LOCAL) and purposely making an increment to it and telling Java:
"If this variable == 0, then execute this code"
so that even if that method is called the second time, that block of code won't execute because the variable has changed and is no longer zero... and it will never again be zero because it keeps increasing.
public void actionPerformed(ActionEvent e){
if(e.getSource()==deal){/*do something*/}
}
My problem is that the if statment executes more than once when I press the button "deal".
Try something like:
public class Test {
private boolean isExecuted;
public synchronized void executeOnce() {
if (isExecuted) {
return;
} else {
//do your stuff
isExecuted = true;
}
}
}
Modify it as per your requirement. To improve performance, you can use double checked locking.
Try this way, It's dummy code but through this way, you can execute inner for loop code only once.
List<WebElement> allelements = driver.findElements(By.id("id1"));
int i = 0;
for (WebElement e : allelements)
{
i++;
List<WebElement> secondelements = driver.findElements(By.id("id2"));
if(i==1)
{
for(WebElement ae : secondelements)
{
System.out.println(ae.getText());
}
}
System.out.println(e.getText());
}
I've been working on my college assignment about stacks in java, but I can't seem to find the help for this assignment. It's about switchbox routing and I'm required to use stack for this assignment. The assignment is due tomorrow, but there's no need to rush for answers since I'm only looking for the solutions and not the grades. Since the problem isn't written in english, I'm going to give short explanation.
The switchbox contains 4n pins, with n pins on each side of the box. The switchbox is routable only when none of the lines connecting a pair of pins intersect another. The input will be pairs of connected pin numbers.
A few possible solutions I've found but can't understand:
1. The problem can be solved with a similar algorithm as the one for the parentheses matching
2. Putting the pin numbers in stack and an array and compare them (this is the most confusing one)
My code so far (trying the second algorithm):
import java.util.Scanner;
import java.util.Stack;
public class Tester {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int cases=sc.nextInt();
for(int i=0;i<cases;i++){
int pin=sc.nextInt();
SwitchBox box=new SwitchBox(pin);
for(int j=0;j<pin*4;j++){
box.add(sc.nextInt());
}
boolean res=box.isRoutable();
if(res){
System.out.println("routable");
}
else{
System.out.println("not routable");
}
}
}
static class SwitchBox{
Stack<Integer> pins;
int[] pairs;
int[] comparator;
public SwitchBox(int pin){
this.pins=new Stack<Integer>();
this.pairs=new int[(pin*4)];
this.comparator=new int[this.pairs.length];
}
public boolean isRoutable(){
Stack<Integer> s=new Stack<Integer>();
for(int i=0;i<pairs.length;i++){
pairs[i]=pins.peek();
s.push(pins.pop());
}
for(int i=pairs.length-1;i>=0;i--){
if(pairs[i]!=s.pop()){
return true;
}
}
return false;
}
public void add(int pinNum){
if(pins.isEmpty()){
pins.push(pinNum);
}
else{
Stack<Integer> temp=new Stack<Integer>();
int top=(int)pins.peek();
while(top>pinNum){
temp.push(pins.pop());
if(pins.isEmpty()) top=pinNum;
else top=(int)pins.peek();
}
pins.push(pinNum);
while(!temp.isEmpty()){
pins.push(temp.pop());
}
}
}
}
}
In your add method else block is wrong. I was unable to understand what you try to do but you need to do next thing
public void add(int pinNum) {
if (pins.isEmpty()) {
pins.push(pinNum);
} else {
Integer last = pins.peek();
if (last == pinNum) {
pins.pop();
} else {
pins.push(pinNum);
}
}
}
After that isRoutable method just need to check pins stack. If it empty, then all fine. Else there are intersecting lines.
Ok, so I am going to see if this makes sense. In the second method below (int numAdd) I want to be used for the private method (int searchingNum). I don't really understand how private methods work, but whatever number the user enters for the (int numAdd) I want to be duplicated for the parameters in the first method. How is this possible?
//See if user input returns -1 to test whether or not number exists in the array
private int indexOf(int searchingNum)
{
}
//Add number in the array
public boolean addNumber(int numAdd)
{
if (list.contains(numAdd))
{
return false;
}
else
{
list.add(numAdd);
count++;
return true;
}
}
that's it? indexOf(numAdd);
public boolean addNumber(int numAdd)
{
// somewhere, in the middle of nowhere
indexOf(numAdd);
// more of code
}
You can call method of same class directly. No need to do anything. Like this :
public boolean addNumber(int numAdd)
{
int abc = indexOf(numAdd);
//Whatever you want to do...
}
I have the following code
public void alearningrate(){
//adopt learningrate
System.out.println(errorpositive+""+learningrate);
if ((errorpositive&&errorgradient>0)||!errorpositive&&errorgradient<0){
learningrate=learningrate*1.5;
}else{
learningrate=learningrate*0.75;
}
if (learningrate<0.0001){
learningrate=0.0001;
}
}
public void lastblame(double blame){
errorpositive=errorgradient>0;
errorgradient= Math.exp(-value)/Math.pow(1+Math.exp(-value), 2)*blame;
if (value==Double.NaN){
System.out.println("nan detected=?");
errorgradient=1;
}
for (int i=0; i<weight.length;i++){
weight[i]=weight[i]+learningrate*source[i].output()*errorgradient;
}
//alearningrate();
}
public void hiddenblame(double blame){
System.out.println(value);
errorpositive=errorgradient>0;
errorgradient=Math.exp(-value)/Math.pow(1+Math.exp(-value), 2)*blame;
if (value==Double.NaN){
System.out.println("nan detected=?");
errorgradient=1;
}
for (int i=0; i<weight.length;i++){
weight[i]=weight[i]+learningrate*source[i].output()*errorgradient;
}
System.out.println(errorgradient);
alearningrate();
}
however this causus the errorgradient to quickly become NaN, but if I decide to remove the alearningrate() then the code runs fine. I looked at my functions errorgradient function and it should never yield nan, anybody know why I keep getting NaN?