Question

package Week7_Quiz; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.Label;

package Week7_Quiz;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Week7_Quiz extends Application {
private TextField tf1;
private Label stckr;
private int revrsdNum;
private Button btn;
int num = 0;
int reverse;

public static void main(String[] args) {
  
  
launch (args);
}

@Override
public void start(Stage stage) throws Exception {
tf1 = new TextField();
tf1.setLayoutX(10);
tf1.setLayoutY(50);
  
stckr =new Label ("Result: ");
stckr.setLayoutX(12);
stckr.setLayoutY(100);
  
btn = new Button("Reverse");
btn.setLayoutX(10);
btn.setLayoutY(250);
btn.setOnAction(this::revrsdNum);
  
Group gp = new Group(tf1, stckr, btn);

Scene scene = new Scene(gp, 500, 650);
  
stage.setScene(scene);
stage.setTitle("Week 7 Quiz");
stage.show();
  
}

private void revrsdNum(ActionEvent t) { // need help fixing while loop
while(num != 0){
int dig = num%10;
revrsdNum = reverse*10+dig;
num/=10;
revrsdNum = revrsdNum + tf1.getText().charAt(num);

}
  
stckr.setText("Result: " + revrsdNum);

}
  
}

I am trying to solve using this while loop but its giving me error. I need help fixing while loop and run the GUI code. Please help!! thank you

Homework Answers

Answer #1

If you have any problem with the code feel free to comment. I'm assuming that the program is to reverse a number. I have uploaded the rectified method please replace it with your own, the rest of the program remains same.

revrsdNum method

    private void revrsdNum(ActionEvent t) { // need help fixing while loop
                //num was not initialized 
                num = Integer.parseInt(tf1.getText());
                //setting the revrsdNum to 0 every time this method is called
                revrsdNum = 0;
                while (num != 0) {
                        int dig = num % 10;
                        //revrsdNum will be multiplied with 10 not reverse
                        revrsdNum = revrsdNum * 10 + dig;
                        num /= 10;
                }
                stckr.setText("Result: " + revrsdNum);
        }

Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
(JAVA) Use the basic shapes you learned in this chapter to draw a house. Be sure...
(JAVA) Use the basic shapes you learned in this chapter to draw a house. Be sure to include at least two windows and a door code so far: import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; public class House extends Application { @Override public void start(Stage stage) { //Creating a Path Path path = new Path(); Rectangle = new rectangle(20,220); //Moving to the starting point MoveTo moveTo = new MoveTo(160,20);    //Creating 1st line...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of the SAX parser, I am having trouble changing my code in order to utilize the DOM parser to extract the XML data into the tree structure. Would you be able to explain what changes should be made to the code in order to use the DOM parser instead of the SAX parser? // Java Packages //      import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import...
Based on Movie package example, you will create a package about your favorite TV series and...
Based on Movie package example, you will create a package about your favorite TV series and provide the plot() Readme.txt: Explain how you apply polymorphism in your program package Movie; public class MovieTester { public static void main(String[] args) { for(int i = 1; i < 11; i++){ Movie movie = randomMovie(); System.out.println("Movie # " + i + ": " + movie.getTitle() + ", Genre: " + movie.getGenre() + '\n' +"Plot: " + movie.plot()); } } public static Movie randomMovie(){...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT