2016-07-24 127 views
0

我想在java中構建一個非常基本的程序來打印字符串中的所有唯一字符,但我得到運行時錯誤。
獲取運行時錯誤

Input - amanda 
output -amnd 


import java.util.*; 
class uniquechars { 
    public static void main(String[] args) { 
     Scanner inp = new Scanner(System.in); 
     System.out.print("Enter a string:"); 
     String str = inp.nextLine();    // input from user 
     String res=""; 
     for (int i=0;i<str.length();i++){ 
      int count=0; 
      for(int j=0;j<res.length();j++){ 
       if(str.charAt(i)==res.charAt(j)){ 
        count++; 
       } 
      } 
      if(count==0){ 
       res = res+str.charAt(i); 
      } 
     } 
     System.out.println("Output string with only unique characters:"+res); 

    } 
} 

錯誤

Exception in thread "main" java.util.NoSuchElementException: No line found 
    at java.util.Scanner.nextLine(Scanner.java:1540) 
    at uniquechars.main(Main.java:6) 
+0

添加您正在獲取的運行時錯誤。 –

+0

我添加了錯誤,請參閱 – Nishtha

+0

@Nishtha - 您是否嘗試在控制檯中輸入內容? – TheLostMind

回答

1

如果您使用任何在線工具對代碼進行測試時,一定要提供輸入到程序。 我的猜測是,你忘記在線工具上運行程序時將輸入提供給程序。

+0

它不工作仍然得到相同的錯誤 – Nishtha

+0

https://www.codechef.com/ide我跑過來這裏請參閱 – Nishtha

+1

@Nishtha編輯我的答案。在你提到的工具中,右下角有一個複選框(說:自定義輸入)。選中該複選框並提供您的輸入。你的程序應該工作。 –

1

它適用於codechef.com/ide,您只需從下拉列表中選擇您的編程語言即可。如此處所示。

enter image description here