-3

I am trying to figure out how to convert text from text fields into intergers in Eclipse. here is what I have so far, and it's been a few days since I have worked on this program and I am also new to java coding. If I have the code I need in here already, I apologize in advance for asking this question.

protected void do_enterButton_actionPerformed(ActionEvent arg0) {
    int scr1, scr2, scr3 = -1;
    TestScores score = (TestScores)studentList.getSelectedValue();
    int score1 = Integer.parseInt(score1TextField.getText());
    score.setScore1(score1);
    int score2 = Integer.parseInt(score2TextField.getText());
    score.setScore2(score2);
    int score3 = Integer.parseInt(score3TextField.getText());
    score.setScore3(score3);

    if (score1TextField != null) {
        // this is where I need to convert to text to integer
    }
Dan
  • 3
  • 3

2 Answers2

0

score1 is where you already did the conversion. Integer.parseInt(score1TextField.getText()); was correct.

einnocent
  • 3,567
  • 4
  • 32
  • 42
  • thank you! last time I tried it, it gave me an error, but now I think I know why – Dan Apr 03 '14 at 14:58
  • Perhaps the string wasn't parseable into an integer, or the `getText()` call was unnecessary or failed on a `null`. In fact, I don't even see where `score1TextField` gets declared. I'm surprised you didn't get a compiler error. – einnocent Apr 03 '14 at 14:59
  • sorry, I have A LOT of code and didn't want to bore people with all of it, text fields are declared at the start of the program so everything can access them when needed – Dan Apr 03 '14 at 15:04
0

The Integer.parseInt() you have in there converts text (String values or char sequences) to integer.

You should encase those in a try-catch block though.

    try{
    test = Integer.parseInt(textval);
    }catch(NumberFormatException e){
    System.out.println("A number was expected but not found.");
    test = 0;
    }
SvenT23
  • 667
  • 6
  • 19