2017-02-28 46 views
1

我想寫一個實際上是一個堆棧的程序。給定一個包含特定關鍵字的文本文件,我希望我的程序逐行評估文本並對堆棧執行所請求的操作。
如何從字符串和整數文本文件中提取int值?

例如,如果輸入文件是:

push 10 

push 20 

push 30 

產生的堆棧應該是這樣的:

30 

20 

10 

不過,我不知道如何將這些值推入堆棧不需要在單詞push後面硬編碼一個int值。我做了一個字符串變量,並將其分配給scanner.nextLine()

從那裏,我比較符合strLine中:如果strLine中等於push其次是一些數字,那麼這個數字將被壓入堆棧。

但是,看起來方法nextInt()沒有從輸入流中獲取該數字。

Scanner input = new Scanner(file) 
int number; 
String strLine; 
      while (input.hasNextLine()){ 
      strLine = input.nextLine(); 

      number = input.nextInt(); 
      if(strLine.equals("push " + number)){ 
       stack.push(number); 
      } 

我該如何解決這個問題?
謝謝。

+0

這不是你的完整代碼,哪裏是你的'stack'對象initiasation – Smit

+0

'input.nextLine'讀取整條生產線,其中包括數。你可以做的是使用'input.next()'得到「push」和'input.nextInt()'來獲得數字。 –

+0

謝謝大衛,這工作! – Tyler

回答

0

改變這一點:

number = input.nextInt(); 

這樣:

number = Integer.parseInt(input.nextLine()); 
0

如果我理解你的問題,我只想通過拆分上空白標記化行了。

它看起來像你的輸入是相對結構化的:你有一種關鍵字,然後空白,然後一個數字。如果您的數據確實具有這種結構,請將該行分成兩個標記。讀取第二個值。例如:

String tokens[] = strLine.split(" "); 
// tokens[0] is the keyword, tokens[1] is the value 
if(tokens[0].equals("push")){ 
    // TODO: check here that tokens[1] is an int 
    stack.push(Integer.parseInt(tokens[1])); 
} else if (tokens[0].equals("pop")) { // maybe you also have pop 
    int p = stack.pop(); 
} else if ... // maybe you have other operations 
+1

'if(tokens [0] ==「push」){'OH真的嗎? –

+0

另外'分裂'不採取'char' –

+0

謝謝!是的,顯然我很少寫Java。感謝您的改進。我希望OP能夠看到這一點,那就是自輸入格式結構化以來,你應該自己解析這條線。 – JawguyChooser

1

獲取輸入並用空格「」分割! 這會給[[push],「1」] 將第一個索引轉換爲int,然後將該值推入堆棧!

while (input.hasNextLine()){ 
      String[] strLine = input.nextLine().split(" "); 
      if(strLine[0].equals("push")){ 
       stack.push(Integer.parseInt(strLine[1])); 
      } 
      else if(strLine[0].equals("pop")){ 
       stack.pop(); 
      } 
      else{ 
       system.out.println("Please enter a valid input!"); 
      } 
    } 

希望它有幫助!

+1

'if(strLine [0] ==「push」){'OH真的嗎? –

+0

是的,我明白了。它應該是平等的!我編輯了! –

+0

完全沒有!我只是錯過了該評論的感謝.. :) –

0

nextLine方法解析整行,包括行中的任何數字。因此,您需要注意分割線並解析代碼中的數字。

像下面的東西會工作在我用空格分隔線的地方。雖然有很多這樣的可能。

Scanner input = new Scanner(file); 
    String strLine; 
    Stack<Integer> stack = new Stack<>(); 
    int number; 
    while (input.hasNextLine()){ 
     strLine = input.nextLine(); 
     if(strLine.startsWith("push")){ 
      String[] sArr = strLine.split("\\s+"); 
      System.out.println(strLine); 
      if(sArr.length==2){ 
       number=Integer.parseInt(sArr[1]); 
       stack.push(number); 
       System.out.println(number); 
      } 
     } 
    } 
1

input.nextLine讀取整行,包括數字。你可以做的是使用input.next()來獲得「推」和input.nextInt()獲得該號碼。這個例子使用Scanner和System.in(所以它需要「退出」來退出while循環),但它也應該與一個文件一起工作(在這種情況下,你不需要輸入「quit」來退出程序,因爲它會在輸入文件沒有更多輸入時自動執行)。使用parseInt(正如其他一些答案所建議的)的優點是您可以使用try/catch塊捕獲整數輸入中的任何錯誤。

import java.util.Scanner; 
import java.util.Stack; 
import java.util.InputMismatchException; 

public class StackScanner { 
    public static void main(String args[]) { 
     Stack<Integer> stack = new Stack<Integer>(); 

     Scanner input = new Scanner(System.in); 
     int number; 
     String strLine; 
     while (input.hasNext()){ 
      strLine = input.next(); 

      if(strLine.equals("push")){ 
       try { 

        number = input.nextInt(); 
        stack.push(number); 
       } catch (InputMismatchException e) { 
        System.out.println("Invalid input. Try again."); 
        input.nextLine(); 
        continue; 
       } 
      } else { 
       break; 
      } 
     } 
     System.out.println(stack); 
    } 
} 

輸出示例:

push 5 
push 6 
push 3 
quit 
[5, 6, 3] 
+0

出於某種原因,我可以推2.5,3.3等值...我怎樣才能防止這種情況? – Tyler

+0

@泰勒我無法重現您的問題。如果我輸入一個像2.5或3.3的值,我會得到一個InputMismatchException,我可以通過將它包含在try/catch塊中來防止這個異常。 –

+0

@Tyler我編輯了我的答案,包括一個try-catch塊,它將捕獲InputMismatchException。 –

相關問題