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);...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
1) ADD a button that says "Change Pictures" 2) WRITE some code to handle the button...
1) ADD a button that says "Change Pictures" 2) WRITE some code to handle the button event (in other words, when you press the button, this is the code that it goes to). In that code, change the pictures. JAVA CODE: import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.GridPane; import javafx.geometry.Insets; import javafx.scene.image.Image; import javafx.scene.image.ImageView; /** * This program demonstrates the GridPane layout container. */ public class GridPaneImages extends Application { //THESE ARE global (vs local) variables // which means...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
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...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static void readFromFile() throws IOException { BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line != null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); System.out.println("Content of ArrayLiList:"); // split by new line int num = 0; for (String line1 : listOfLines) { String line2[] = line1.split(","); // int burstTime = Integer.parseInt(line2[3].trim()); // String retrival = listOfLines.get(0); System.out.println("...
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(){...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer>...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer> moneyBox; //store the log of transactions for auditing StringBuilder log; /** * Constructs an empty register */ public SimpleRegister() { moneyBox = new TreeMap<Integer, Integer>(); moneyBox.put(1, 0); moneyBox.put(5, 0); moneyBox.put(10, 0); moneyBox.put(25, 0); moneyBox.put(100, 0); moneyBox.put(500, 0); moneyBox.put(1000, 0); log = new StringBuilder(); } @Override public void addPennies(int num) { moneyBox.put(1, moneyBox.get(1) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 1 / 100.0f);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT