gpt4 book ai didi

java - 线程 "main"java.util.NoSuchElementException 与 Scanner.nextInt() 中出现异常

转载 作者:行者123 更新时间:2023-12-02 09:53:59 36 4
gpt4 key购买 nike

我在执行以下双重转置加密和解密代码时遇到问题。

import java.io.*;
import java.util.*;

public class HelloWorld
{
public static void main(String[] args)
{
NewEncryptAndDecrypt ned=new NewEncryptAndDecrypt();
ned.input();
}
}

class NewEncryptAndDecrypt
{
Scanner sc=new Scanner(System.in);
void input()
{
int ch;
do
{
System.out.println("\n\t\t*****ENTER*****");
System.out.println("\t1.Encrypt");
System.out.println("\t2.Decrypt");
System.out.println("\t0.Exit");
ch=sc.nextInt();
switch(ch)
{
case 1:
{
encrypt();
break;
}
case 2:
{
decrypt();
break;
}
}
}while(ch!=0);
}
void encrypt()
{


//String ip,t;
char text[][],enc[][];
int key,i,j,row[],col[],r,c;
int k=0,m,n;
System.out.print("Enter The Plain Text ");
StringBuilder ip = new StringBuilder(sc.next());
StringBuilder t = new StringBuilder(sc.nextLine());
// ip=sc.next();
// t=sc.nextLine();
System.out.print("Enter The Number Of Rows ");
r=sc.nextInt();
System.out.print("Enter The Number Of Columns ");
c=sc.nextInt();
text=new char[r][c];
enc=new char[r][c];
row=new int[r];
col=new int[c];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
text[i][j]=ip.charAt(k);
k++;
}
}
System.out.println("Enter The Row Key ");
for(i=0;i<r;i++)
row[i]=(sc.nextInt()-1);
System.out.println("Enter The Column Key ");
for(i=0;i<c;i++)
col[i]=(sc.nextInt()-1);
k=0;
System.out.print("The Cipher Text Is ");
for(i=0;i<r;i++)
{
m=row[i];
for(j=0;j<c;j++)
{
n=col[j];
enc[i][j]=text[m][n];
System.out.print(""+Character.toUpperCase(enc[i][j]));
}
}
}
void decrypt()
{
String ip,t;
char text[][],enc[][];
int key,i,j,row[],col[],r,c;
int k=0,m,n;
System.out.print("Enter The Cipher Text ");
ip=sc.next();
t=sc.nextLine();
System.out.print("Enter The Number Of Rows ");
r=sc.nextInt();
System.out.print("Enter The Number Of Columns ");
c=sc.nextInt();
text=new char[r][c];
enc=new char[r][c];
row=new int[r];
col=new int[c];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
text[i][j]=ip.charAt(k);
k++;
}
}
System.out.println("Enter The Row Key ");
for(i=0;i<r;i++)
row[i]=(sc.nextInt()-1);
System.out.println("Enter The Column Key ");
for(i=0;i<c;i++)
col[i]=(sc.nextInt()-1);
k=0;
for(i=0;i<r;i++)
{
m=row[i];
for(j=0;j<c;j++)
{
n=col[j];
enc[m][n]=text[i][j];
}
}
System.out.print("The Retrieved Plain Text Is ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(""+Character.toLowerCase(enc[i][j]));
}
}
}
}

我从调用scanner.nextInt()的函数input()收到错误

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at NewEncryptAndDecrypt.input(HelloWorld.java:33)
at HelloWorld.main(HelloWorld.java:17)

这里出了什么问题?我怀疑这是 Scanner.nextInt() 或此处缓冲区的问题,但我真的不明白我在互联网上阅读的内容。

最佳答案

如果没有更多可用 token ,将抛出

NoSuchElementException

在为变量赋值之前,您应该使用hasNextInt()

if(sc.hasNextInt()){
ch=sc.nextInt();
}

关于java - 线程 "main"java.util.NoSuchElementException 与 Scanner.nextInt() 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56138781/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com