Посмотрите пожалуйста, может кто знает, как исправить "NoSuchElementException: No line found" ошибку. Когда я запускаю программу в IntelliJ на Java 17 на своем компьютере, то подобную ошибку не дает. Но когда преподаватель проверяет этот код и запускает на своем компьютере, а также когда я пробовала запустить код на Jdoodle (https://www.jdoodle.com/online-java-compiler/) то дает эту ошибку:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Assembler.main(Assembler.java:34)
Программа:
import java.util.Map;
import java.util.Scanner;
public class Assembler
{
private static final boolean TEST_MODE = !true;
private static final Map<String, Byte> opcodes = Map.of(
"ADD", (byte) 0b100000
, "AND", (byte) 0b100100
, "ADDI", (byte) 0b001000
, "ANDI", (byte) 0b001100
, "BEQ", (byte) 0b000100
, "LW", (byte) 0b100011
, "SW", (byte) 0b101011
, "SRL", (byte) 0b000010
, "SLL", (byte) 0b000000
);
public static void main(String[] args)
{
int count = 0;
if (TEST_MODE)
{
testCases();
} else
{
System.out.println("Assembler - name\n");
System.out.println("*** Begin entering Assembler:\n");
while (true)
{
var scanner = new Scanner(System.in);
var line = scanner.nextLine();
if (line.equalsIgnoreCase("HALT"))
{
break;
}
count++;
System.out.println("***: " + String.format("%32s",
Integer.toBinaryString(make(line))).replace(" ", "0"));
}
System.out.println("\n*** Assembly complete. Program required " + count + " words of memory.");
}
}
private static int make(String line)
{
var parts = line.split("\\s+");
var funct = parts[0];
switch (funct)
{
case "ADD", "AND":
return AddOrAnd(funct, parts[2], parts[3], parts[1]);
case "ADDI", "ANDI":
return AddiOrAndi(funct, parts[2], parts[1], parts[3]);
case "BEQ":
return Beq(funct, parts[1], parts[2], parts[3]);
case "SW", "LW":
return SwLw(funct, parts[3], parts[1], parts[2]);
case "SLL", "SRL":
return Sll(funct, "0", parts[2], parts[1], parts[3]);
default:
throw new UnsupportedOperationException(funct);
}
}
private static int SwLw(String funct, String rs, String rt, String im)
{
var n = makeI(opcodes.get(funct), regToByte(rs), regToByte(rt), Short.parseShort(im));
return n;
}
private static int Sll(String funct, String rs, String rt, String rd, String sa)
{
var n = makeR((byte) 0, Byte.parseByte(rs), regToByte(rt), regToByte(rd), Byte.parseByte(sa), opcodes.get(funct));
return n;
}
private static int Beq(String funct, String rs, String rt, String im)
{
return makeI(opcodes.get(funct), regToByte(rs), regToByte(rt), Short.parseShort(im));
}
private static int AddiOrAndi(String funct, String rs, String rt, String im)
{
return makeI(opcodes.get(funct), regToByte(rs), regToByte(rt), Short.parseShort(im));
}
private static int AddOrAnd(String func, String rs, String rt, String rd)
{
var opcode = (byte) 0;
var sa = (byte) 0;
return makeR(opcode, regToByte(rs), regToByte(rt), regToByte(rd), sa, opcodes.get(func));
}
private static int makeR(byte opcode, byte rs, byte rt, byte rd, byte shiftAmount, byte funct)
{
int returnValue = (opcode << 26) + (rs << 21) + (rt << 16) + (rd << 11) + (shiftAmount << 6) + funct;
return returnValue;
}
private static int makeI(byte opcode, byte rs, byte rt, short immed)
{
int intImmed = immed;
if (immed == -1)
{
intImmed = (immed >>> (Short.BYTES * Short.SIZE - 16));
}
int returnValue = (opcode << 26) + (rs << 21) + (rt << 16) + intImmed;
return returnValue;
}
private static byte regToByte(String r)
{
byte returnValue = 0;
String upperCase = r.toUpperCase();
switch (upperCase) {
case "$AT":
returnValue = 1;
break;
case "$ZERO":
returnValue = 0;
break;
case "$V0":
returnValue = 2;
break;
case "$V1":
returnValue = 3;
break;
case "$A0":
returnValue = 4;
break;
case "$A1":
returnValue = 5;
break;
case "$A2":
returnValue = 6;
break;
case "$A3":
returnValue = 7;
break;
case "$T0":
returnValue = 8;
break;
case "$T1":
returnValue = 9;
break;
case "$T2":
returnValue = 10;
break;
case "$T3":
returnValue = 11;
break;
case "$T4":
returnValue = 12;
break;
case "$T5":
returnValue = 13;
break;
case "$T6":
returnValue = 14;
break;
case "$T7":
returnValue = 15;
break;
case "$T8":
returnValue = 24;
break;
case "$T9":
returnValue = 25;
break;
case "$S0":
returnValue = 16;
break;
case "$S1":
returnValue = 17;
break;
case "$S2":
returnValue = 18;
break;
case "$S3":
returnValue = 19;
break;
case "$S4":
returnValue = 20;
break;
case "$S5":
returnValue = 21;
break;
case "$S6":
returnValue = 22;
break;
case "$S7":
returnValue = 23;
break;
}
return returnValue;
}
/*************************************************************
*
* Test Code below
* Do NOT modify
*
************************************************************/
private static void tests_make()
{
test_make("ADD $v0 $v1 $zero", 0b00000000011000000001000000100000);
test_make("AND $a0 $a1 $a2", 0b00000000101001100010000000100100);
// test_make("ADDI $a3 $t4 -321", 0b00100001100001111111111010111111);
test_make("ADDI $a3 $t4 3", 0b00100001100001110000000000000011);
test_make("ANDI $t0 $t5 123", 0b00110001101010000000000001111011);
test_make("BEQ $s0 $t1 +517", 0b00010010000010010000001000000101);
// test_make("LW $s1 -12 $t2", 0b10001101010100011111111111110100);
test_make("LW $s1 12 $t2", 0b10001101010100010000000000001100);
test_make("SW $s2 20 $t3", 0b10101101011100100000000000010100);
test_make("SRL $a2 $a3 3", 0b00000000000001110011000011000010);
test_make("SLL $a3 $a2 31", 0b00000000000001100011111111000000);
}
private static boolean test_make(String cmd, int expected)
{
// funct rd rs rt
var n = make(cmd);
var a = Integer.toBinaryString(n);
if (n != expected)
{
System.out.println("make failed for " + cmd);
return false;
}
return true;
}
private static void testCases()
{
tests_make();
if (test_regToByte())
System.out.println("RegToByte working well\n");
else
System.out.println("RegToByte failed\n");
if (test_makeR())
System.out.println("makeR working well\n");
else
System.out.println("makeR failed\n");
if (test_makeI())
System.out.println("makeI working well\n");
else
System.out.println("makeI failed\n");
}
private static boolean test_regToByte() {
boolean passedTest = true;
String[] regs = {"$ZERO", "$AT", "$V0", "$V1", "$A0", "$A1", "$A2", "$A3", "$T0", "$T1", "$T2", "$T3", "$T4",
"$T5", "$T6", "$T7", "$S0", "$S1", "$S2", "$S3", "$S4", "$S5", "$S6", "$S7", "$T8", "$T9"};
for (int i = 0; i < regs.length; i++) {
passedTest &= testReg(regs[i], i);
}
return passedTest;
}
private static boolean testReg(String regName, int expValue) {
if (regToByte(regName) != expValue)
System.out.printf("Fail: regToByte(\"%s\") = %d, should be %d\n", regName, regToByte(regName), expValue);
return regToByte(regName) == expValue;
}
private static boolean test_makeR() {
boolean passedTest = true;
int i = makeR((byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1);
if (i != 0b00000100001000010000100001000001) {
System.out.println("Test: makeR(1,1,1,1,1,1) = " + Integer.toBinaryString(i) + " failed");
passedTest = false;
}
i = makeR((byte) 63, (byte) 0, (byte) 31, (byte) 0, (byte) 31, (byte) 0);
if (i != 0b11111100000111110000011111000000) {
System.out.println("Test: makeR(63,0,31,0,31,0) = " + Integer.toBinaryString(i) + " failed");
passedTest = false;
}
return passedTest;
}
private static boolean test_makeI() {
boolean passedTest = true;
int i = makeI((byte) 1, (byte) 1, (byte) 1, (short) 1);
if (i != 0b00000100001000010000000000000001) {
System.out.println("Test: makeI(1,1,1,1) = " + Integer.toBinaryString(i) + " failed");
passedTest = false;
}
i = makeI((byte) 63, (byte) 0, (byte) 31, (short) 0);
if (i != 0b11111100000111110000000000000000) {
System.out.println("Test: makeI(63,0,31,0) = " + Integer.toBinaryString(i) + " failed");
passedTest = false;
}
i = makeI((byte) 63, (byte) 0, (byte) 31, (short) -1);
if (i != 0b11111100000111111111111111111111) {
System.out.println("Test: makeI(63,0,31,-1) = " + Integer.toBinaryString(i) + " failed");
passedTest = false;
}
return passedTest;
}
}
Input:
ADD $v0 $v1 $zero
AND $a0 $a1 $a2
ADDI $a3 $t4 -321
ANDI $t0 $t5 123
BEQ $s0 $t1 +517
LW $s1 -12 $t2
SW $s2 20 $t3
SRL $a2 $a3 3
SLL $a3 $a2 31
HALT
Output:
***: 00000000011000000001000000100000
***: 00000000101001100010000000100100
***: 00100001100001111111111010111111
***: 00110001101010000000000001111011
***: 00010010000010010000001000000101
***: 10001101010100011111111111110100
***: 10101101011100100000000000010100
***: 00000000000001110011000011000010
***: 00000000000001100011111111000000
*** Assembly complete. Program required 9 words of memory.