Jump to content

NumberFormatException: For input string: "2 "


riceje7

Recommended Posts

i am getting this error and have never seen it before, just wondering if anyone knows what is it and how i can get rid of it. let me know if you need any other info. my code is below. and the contents of words.txt is a series of words separated my spaces and the contents of nums.txt is a series of random numbers separated by spaces.

 

PFont font1;
PFont font2;
PFont font3;
PFont font4;
PFont font5;
PFont font6;
PFont font7;
PFont font8;
PFont font9;
PFont font10;


float wordx[];
float wordy[];

float[] wordsizearray;
String[] wordsarray;



void setup(){
  size(960,600);
  smooth();
  frameRate(30);
  font1 = loadFont("Helvetica-10.vlw");
  font2 = loadFont("Helvetica-20.vlw");
  font3 = loadFont("Helvetica-30.vlw");
  font4 = loadFont("Helvetica-40.vlw");
  font5 = loadFont("Helvetica-50.vlw");
  font6 = loadFont("Helvetica-60.vlw");
  font7 = loadFont("Helvetica-70.vlw");
  font8 = loadFont("Helvetica-80.vlw");
  font9 = loadFont("Helvetica-90.vlw");
  font10 = loadFont("Helvetica-100.vlw");

  
  String[] tempwords = loadStrings("words.txt");
  //String[] tempwords2 = split(tempwords, " ");
  String[] tempnum = loadStrings("nums.txt");
  //int[] tempnum2 = split(tempnum, " ");
  for(int i = 0; i < tempnum.length;i++){
    wordsizearray[i] = Integer.parseInt(tempnum[i]);
    wordsarray[i] = tempwords[i];
  }
  
  
  wordx = new float [tempwords.length];
  wordy = new float [tempwords.length];

  for(int i=0;i<tempwords.length;i++){
    wordx[i] = random(0, width-200);
    wordy[i] = random(0, height);
    //wordsarray[i] = tempwords[i];
    //wordsizearray[i] = tempnum[i];
  }
}


void draw() {

  background(200);

  float startwordsize = 10;
  float actualwordsize = 0.1;
  // background(200);
  String[] data = loadStrings("words.txt");
  fill(0,0,0,100);
  //textMode(SHAPE);


  for(int i=0; i<data.length;i++){
    String wordsize = Calcwordsize(max(wordsizearray),min(wordsizearray),wordsizearray[i]);
    if(wordsize == "font1"){
      textFont(font1);
    }
    else if(wordsize == "font2"){
      textFont(font2);
    }
    else if(wordsize == "font3"){
      textFont(font3);
    }
    else if(wordsize == "font4"){
      textFont(font4);
    }
    else if(wordsize == "font5"){
      textFont(font5);
    }
    else if(wordsize == "font6"){
      textFont(font6);
    }
    else if(wordsize == "font7"){
      textFont(font7);
    }
    else if(wordsize == "font8"){
      textFont(font8);
    }
    else if(wordsize == "font9"){
      textFont(font9);
    }
    else if(wordsize == "font10"){
      textFont(font10);
    }

    text(data[i], wordx[i],wordy[i]);
    float r = random(-2.5,2.5);
    float z = noise(wordx[i],wordy[i]);
    wordx[i] += r*z;
    wordy[i] += r*z;

  }
}  

Integer.parseInt(tempnum);

 

Is I'm sure what's causing the problem.

 

You can change that to:

 

Integer.parseInt(tempnum.trim());

 

To fix the problem with "2 " (notice the following space).

 

But, if you want to detect errors without it failing, you can wrap the code in a try catch block:

 

try {

    Integer.parseInt(tempnum);

}

catch(NumberFormatException e) {

    //do something

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.