2016-04-14 7 views
0

私は次の行を定義したテキストファイルを持っています:Hi 0x01.私は単語Hiを読み込み、それ自身の変数に0x01という変数を格納しようとしています。問題I 「を有するM Iは正直にあなたがここで何をしようとして見当がつかない私はHiで読み取ることができるように見えるということですが、私はできるt read in 0x01`.Hereは私のコードスキャナを使用してテキストファイルを読み取る

File comms =new File("src/Resources/com.txt"); 
     try (Scanner scan = new Scanner(comms)) { 
      while (scan.hasNext()) { 
       String line = scan.nextLine(); 
       Scanner sc = new Scanner(line); 
       sc.useDelimiter("\\s+"); 
       try { 
        String comm1 = sc.next(); 
       // System.out.println(comm1); 
       int value =sc.nextInt(); 
       System.out.println(value); 
       sc.close(); 
       } catch (Exception ef){ 

       } 

答えて

2

されます。あなたはより良いそれを一度にスキャンしたい:

File comms = new File("src/Resources/com.txt"); 
    try(Scanner scan = new Scanner(comms)) { 
     while(scan.hasNext()) { 
      String line = scan.nextLine(); 

      String[] words = line.split(" "); 

      System.out.println(words[0]); // "Hi" 
      System.out.println(words[1]); // "0x01" 
     } 
    } 
    catch(Exception e) { 

    } 

今、別の文字列でこれらを持つことは、あなたが[1]はintに変換する言葉のように、それを世界に何かを行うことができます。

+0

ありがとう、これは私が欲しかったものです。 – tashtoons

関連する問題