-2

I have tried the other solutions available online but none of them work for me for some reason. The conversion to the string is working but then when i try converting that string to an integer it does not work

String cs = (janout.getText().to string()); Integer yo = Integer.valueOf(cs);

Janout is a Text View getting it's value from a text input

This code isn't working for some reason, I think it has something to do with the string being a variable

  • use Integer.parseInt(String str) – Roophie Jun 24 '21 at 12:50
  • 2
    Does this answer your question? [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – azthorgis Jun 24 '21 at 12:51
  • ParseInt also hasn't worked – chris martin Jun 24 '21 at 12:57
  • @chrismartin: Provide a working example. Provide the exception or compilation/syntax error. – peterulb Jun 24 '21 at 13:05
  • I have a catch clause so the code just doesn't return a value, which tells me that there's a problem with the number format but how can that be possible if im entering a digit – chris martin Jun 24 '21 at 13:12
  • I wonder if there's something special I'm supposed to do if the string being converted to an integer is a variable – chris martin Jun 24 '21 at 13:14
  • The `NumberFormatException` that you probably get would tell you what the problem is (the message contains a descriptive text like `For input string: "xyz"`) which means that in the catch clause you should at least add a `System.out.println(e);` – Thomas Kläger Jun 24 '21 at 13:30

2 Answers2

0
    public class StringToInt {

    public static void main(String[] args) {
        
        String sNumber = "12";
        int iNumber = Integer.parseInt(sNumber);
        
        // Perform addition to prove it worked
        System.out.println(iNumber + 1);
 
    }
    
}
Jamez
  • 41
  • 1
  • 4
  • my string is a variable, and it seems all these solutions work for strings with set values but in my case it's not – chris martin Jun 24 '21 at 12:59
  • Have re-posted an answer using JavaFX, but the line of code you're most interested in will likely be: int n = Integer.parseInt(tf.getText()); – Jamez Jun 24 '21 at 13:24
0

It looks like you're using .getText() so I'm assuming your working with a GUI. How about this using JavaFX?

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class StringToIntTextField extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        TextField tf = new TextField();
        Button btn = new Button();
        btn.setPrefWidth(150);
        btn.setText("Convert to Int'");
        
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                
                int n = Integer.parseInt(tf.getText());
                System.out.println(n + 1);
            }
        });
        
        GridPane gp = new GridPane();
        gp.add(btn, 0, 1);
        gp.add(tf, 0, 0);
        
        Scene scene = new Scene(gp, 150, 75);
        
        primaryStage.setTitle("String -> Int");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

This will only work if an actual integer is typed. If it is empty or a word/mixed data it will crash. You'll want to use a try/catch for InputMismatch Exception to fix this.

Jamez
  • 41
  • 1
  • 4