在Eclipse中导入java.util.Scanner问题

时间:2018-03-07 18:43:29

标签: java eclipse

技术:

MS Windows 10,

Eclipse IDE(氧气?主要从网站下载)

Java JDK 9.0.4和Java运行时环境

随着Oracle扫描确认Java已经建立,我能够编写介绍性的" Hello World"用Eclipse。在开始之前,有人可以确认这是正确的堆栈下载。一位朋友告诉我,我的问题可能是由于JDK下载问题,但我一直无法解决这个问题。 Anywho ......

我是Java的新手,所以请耐心等待。我正在尝试创建一个简单的素数测试。我研究创建输入的方式是通过Scanner类。但是,无论出于何种原因,系统似乎都无法识别我的班级。

import java.util.Scanner;

public class PrimeTest {

public static void main(String[] args) {
    Scanner n = new Scanner(System.in);
    System.out.println(n.nextLine());
    n.close();
}       

public static boolean isPrime(int n) {
    int sqrt = (int) Math.sqrt(n) + 1;
    for (int i = 2; i < sqrt; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
  } 
}

控制台返回

  

&#34;错误:无法找到或加载主类扫描程序引起:   java.lang.ClassNotFoundException:Scanner&#34;

在做到这一点之前,键入import java.util.不会弹出扫描仪选项 - 但是,它最终&#34;神奇地&#34;出现了。

另外,我在哪里放n值?在控制台?

谢谢 - 我知道这是一个基本问题,但你必须从某个地方开始。

1 个答案:

答案 0 :(得分:1)

关于您的“错误”:
根据经验,我可以告诉你,当某些东西看起来有些不对劲时,它会有一个特殊的神奇时刻,然后它会突然接受它。这是因为有时eclipse的后台进程有点慢,需要一些时间才能实现实际发生的事情。我的建议是使用导入Strg+O或自动完成Strg+Space。通过这种方式,您可以告诉eclipse搜索您需要的内容并从其优惠中进行选择,或者如果它只看到一个可能的选择,它就会自动选择它。然后eclipse立即知道你正在使用什么,它不必等你停止输入并运行后台进程来找出你想要做的事情。

关于您的计划:
对于从/向控制台读/写,java使用InputStreamsOutputStreamsSystem.in指向用于从控制台读取的InputStreamSystem.out指向用于在控制台上写入的OutputStream

System.in已足以从控制台读取。但是,简单的InputStream具有从流中读取的非常基本的方法。使用类似ScannerBufferedReader的包装类更为舒适。

Scanner为您提供了多种方法,可以使用InputStream来阅读nextLine()String。此方法会阻止该过程,直到在控制台上键入换行符(enter)。然后它返回键入为String的所有内容。

System.out.println(...)您现在可以用于多种事情。您可以像使用Scanner scan = new Scanner(System.in); String readLine = scan.nextLine(); scan.close(); 那样将它们打印回控制台,也可以将它们用于进一步操作。为此,我建议您将返回值保存在变量

Integer convertedNumber = Integer.parseInt(readLine);

使用该值,您可以执行进一步的操作,例如将其转换为Integer,例如

isPrime(...)

然后您可以使用它来调用您的函数Server Error in '/' Application. **While trying to resolve a cross-store reference, the SID of the target principal could not be resolved. The error code is 1722.** Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.DirectoryServices.AccountManagement.PrincipalOperationException: While trying to resolve a cross-store reference, the SID of the target principal could not be resolved. The error code is 1722. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [PrincipalOperationException: While trying to resolve a cross-store reference, the SID of the target principal could not be resolved. The error code is 1722.] System.DirectoryServices.AccountManagement.ADStoreCtx.ResolveCrossStoreRefToPrincipal(Object o) +570 System.DirectoryServices.AccountManagement.ADUtils.DirectoryEntryAsPrincipal(DirectoryEntry de, ADStoreCtx storeCtx) +133 System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet.get_CurrentAsPrincipal() +86 System.DirectoryServices.AccountManagement.PrincipalCollectionEnumerator.MoveNext() +252 System.DirectoryServices.AccountManagement.PrincipalCollectionEnumerator.System.Collections.IEnumerator.MoveNext() +9 System.Linq.<SelectManyIterator>d__22`3.MoveNext() +65 System.Linq.Lookup`2.Create(IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) +264 System.Linq.GroupedEnumerable`3.GetEnumerator() +72 System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +63 System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection) +392 System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection) +15 Plastin.Common.Security.ADLDSClaimsProvider.SetApplicationClaims() +1012 Plastin.Common.Security.ADLDSClaimsProvider.Initialise() +174 Plastin.Common.Security.ADLDSClaimsProvider..cctor() +238 [TypeInitializationException: The type initializer for 'Plastin.Common.Security.ADLDSClaimsProvider' threw an exception.] lambda_method(Closure , IBuilderContext ) +81 Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +35 Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +10 Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +198 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +209 Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +165 [ResolutionFailedException: Resolution of the dependency failed, type = "Plastin.Common.Security.IClaimsProvider", name = "(none)". Exception occurred while: Calling constructor Plastin.Common.Security.ADLDSClaimsProvider(). Exception is: TypeInitializationException - The type initializer for 'Plastin.Common.Security.ADLDSClaimsProvider' threw an exception. ----------------------------------------------- At the time of the exception, the container was: Resolving Plastin.Common.Security.ADLDSClaimsProvider,(none) (mapped from Plastin.Common.Security.IClaimsProvider, (none)) Calling constructor Plastin.Common.Security.ADLDSClaimsProvider() ] Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +329 Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) +15 Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, ResolverOverride[] overrides) +72 Plastin.Common.Security.AuthenticationManager.Authenticate(String resourceName, ClaimsPrincipal incomingPrincipal) +149 Plastin.Portal.MvcApplication.Application_PostAuthenticateRequest() +70 [TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +87 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +101 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +21 System.Web.Util.ArglessEventHandlerProxy.Callback(Object sender, EventArgs e) +56 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69