2016-07-24 93 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