Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文件加载时的java.util.InputMismatchException_Java_File_Java.util.scanner_Inputmismatchexception - Fatal编程技术网

从文件加载时的java.util.InputMismatchException

从文件加载时的java.util.InputMismatchException,java,file,java.util.scanner,inputmismatchexception,Java,File,Java.util.scanner,Inputmismatchexception,大家好,我试着用这段代码读取一个文本文件,文本文件应该通过检查下一行的字符串是否以stop、play或duration开头来定义一个音高,然后将其传递给synth,以便它可以播放 有人知道为什么它会导致错误而不起作用吗 代码和示例文本文件如下所示: public class MyTuneRunnable implements Runnable { //method start public void run(){ Thread thr

大家好,我试着用这段代码读取一个文本文件,文本文件应该通过检查下一行的字符串是否以stop、play或duration开头来定义一个音高,然后将其传递给synth,以便它可以播放

有人知道为什么它会导致错误而不起作用吗

代码和示例文本文件如下所示:

      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();
        }
        }
      }
至于文本文件的外观。。。这是一个例子:

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>'
0这是一条注释
0
播放60这是一个C音符,由于“播放”而设置为播放
0
持续时间200表示当前播放的音符将发出声音
0
停止60由于“停止”而停止播放C音符

您的问题源于在同一个文件上使用两个扫描仪。假设当一个扫描器读取一个标记时,两个扫描器都会提前它们的指针,而事实并非如此——只有读取标记的扫描器才会提前,因此,当扫描器指向文本时,您尝试读取int。不要这样做,只使用一台扫描仪


话虽如此,您可以使用多个扫描器,但只能让其中一个扫描器读取文件,这是我经常做的事情:一个扫描器读取文件,通过
nextLine()
获取每行字符串,并为每行文本创建另一个扫描器以提取在该行中找到的标记。当我这样做时,我会注意在完成扫描时关闭每个行扫描程序,当然,在完成扫描后关闭文件扫描程序。

如果您遇到异常并询问它,您会希望将整个stacktrace与您的问题一起发布。它包含重要信息。还要指出哪一行抛出它。为什么在同一个文件上使用两个扫描仪?这很可能是绊倒你的原因,因为在一个方向前进指针不会在另一个方向前进…………喂?