I keep getting this error while attempting to scramble a .wav file in Java: java.lang.NumberFormatException.forInputString(Unknown Source)

StackOverflow https://stackoverflow.com/questions/22123317

  •  18-10-2022
  •  | 
  •  

Pergunta

My code compiles, but when I try to use the method to scramble the .wav file I get an error

Here is the code that is causing the problem:

  public Sound scrambleSound(){
     SoundSample[] sampleArray = this.getSamples();
     ArrayList<Integer> sounds = new ArrayList<Integer>(0);
     String origin = "";
    for(SoundSample s : sampleArray){
      origin = "" + s.getValue();
     for(int i = 0; i<origin.length();i++){

       int n = Integer.parseInt(origin.substring(i,i+1));  //the error is here 

     if(i == origin.length() - 1){
       Integer q = new Integer((int)(Math.pow(3,n))+2);
       sounds.add(q);
     }
     else{
       Integer w = new Integer((int)(Math.pow(3,n))+1);
       sounds.add(w);
     }
    }
    }
     Sound sound1 = new Sound(sounds.size());
     for(int z = 0; z<sounds.size(); z++){
       sound1.setSampleValueAt(z, sounds.get(z).intValue());
     }
     return sound1;
  }
Foi útil?

Solução

You have

for(int i = 0; i<origin.length();i++){

This should be

for(int i = 0; i<origin.length()-1;i++){

because in your substring, you are looking at i+1. You might want to consider using origin.charAt(i) instead, this is the usual way to do it and you will not need to adjust your loop bounds. A substring will work fine with the modification to the loop, however. In addition, you might be trying to turn a character like the - in -1 into an integer, maybe try-catch?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top