简体   繁体   中英

java.util.InputMismatchException When loading from file

Hi so I am trying to use this code to read from a text file, the text file is supposed to define a pitch by checking if the string of the next line starts with either stop, play, or duration and then pass it to synth so it can play.

Does anyone have any idea why it's causing Errors and not working?

The code and an example text file are as follows:

      public class MyTuneRunnable implements Runnable {
      //method start
        public void run(){
           Thread thread = Thread.currentThread();
           thread.getName();
              try {
                    Synthesizer synth = MidiSystem.getSynthesizer();
                    synth.open();
                    MidiChannel[] channels = synth.getChannels();
            File file = new File(Loader.instance().getConfigDir().getParentFile().getAbsolutePath()+"/"+"LoadTunes"+"/"+Config.tuneName+".txt");
            try {
                Scanner intLoader = new Scanner(file);
                Scanner stringLoader = new Scanner(file);
                while (intLoader.hasNextLine()&stringLoader.hasNextLine()) {
                    int i = intLoader.nextInt();
                    String s = stringLoader.next();
                    if (s.startsWith("play")){
                        channels[channel].noteOn( i, volume);
                    }
                    if (s.startsWith("stop")){
                        channels[channel].noteOff( i, volume);
                    }
                    if (s.startsWith("duration")){
                        Thread.sleep(i);
                    }
                }
                intLoader.close();
                stringLoader.close();
            } 
            catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            synth.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        }
      }

As for how the text file looks... this is an example:

0 This is a comment
0
play 60         This is a C note and it is set to play because of 'play <note number>'
0
duration 200    This is saying the currently playing notes will make sound
0
stop 60         This stops playing the C note because of the 'stop <note number>'

Your problem stems from using two scanners on the same file. You're assuming that when one Scanner reads a token, both advance their pointers, and that is not what happens -- only the Scanner that read the token advances, and for this reason, you're trying to read an int when the scanner is pointing to text. Don't do this, use one Scanner only.

Having said this, you could use more than one Scanner, but only have one of them read the file, and this is something I do frequently: one Scanner reads the file, getting each line of String via nextLine() , and another Scanner is created for each line of text to extract the tokens found in the line. When I do this, I take care to close each line Scanner when done with it, and of course close the File Scanner when completely done with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM