我对java有点新,并希望有人能帮助我。我正在使用文本编辑器在虚拟机Ubuntu上编写此代码。如果您想引用该文件,RAM错误就在这里。 http://users.cis.fiu.edu/~crahn/CGS3767/RAMerrors。该错误位于System.out.printf中的公共静态readingLine中。如果有人能帮助我识别错误,我将非常感激。谢谢!
import java.io.*;
import java.util.*;
public class MemoryCalculator
{
private static Scanner convertingFiles;
public static String convertFile = "RAMerrors.txt";
public static void readFile(String nameOfFile) throws IOException
{
convertingFiles = new Scanner(new File(nameOfFile));
}
public static void readingLine(String nameOfFile) throws IOException
{
System.out.println();
int recordingNum = 0;
while(convertingFiles.hasNext())
{
recordingNum = recordingNum +1;
String recordingLine = convertingFiles.nextLine();
System.out.printf("( %d) %g ) \n", recordingNum, recordingLine );
String conv = fromHexToBi(recordingLine);
long decimal = fromBiToDec(conv);
System.out.println(errorRamRangeWeb(decimal));
}
}
public static String fromHexToBi(String input)
{
int fromHexToBi = 0;
String record = "";
char var;
for(int x = 0; x < input.length(); x++)
{
var = input.charAt(x);
if(var == '0')
{
record += "0000";
}
else if (var == '1')
{
record += "0001";
}
else if (var == '2')
{
record += "0010";
}
else if (var == '3')
{
record += "0011";
}
else if (var == '4')
{
record += "0100";
}
else if (var == '5')
{
record += "0101";
}
else if (var == '6')
{
record += "0110";
}
else if (var == '7')
{
record += "0111";
}
else if (var == '8')
{
record += "1000";
}
else if (var == '9')
{
record += "1001";
}
else if (var == 'A')
{
record += "1010";
}
else if (var == 'B')
{
record += "1011";
}
else if (var == 'C')
{
record += "1100";
}
else if (var == 'D')
{
record += "1101";
}
else if (var == 'E')
{
record += "1110";
}
else if (var == 'F')
{
record += "1111";
}
else
{
System.out.print("Sorry, the error is .out of range");
}
}
System.out.println(record);
return record;
}
public static long fromBiToDec(String bi)
{
long decimal = 0;
for(int y = 0; y < bi.length(); y++)
{
if(bi.charAt(y) == '1')
{
decimal = (long) (decimal + Math.pow(2, bi.length() - 1 - y));
}
}
System.out.println(decimal);
return (long) decimal;
}
public static String errorRamRangeWeb(long decimal)
{
String chipRangeFall = "";
long errorRamRange0 = 0;
long errorRamRange1 = 8589934584L;
long errorRamRange2 = 8589934585L;
long errorRamRange3 = 1717986184L;
long errorRamRange4 = 17179869185L;
long errorRamRange5 = 25769803768L;
long errorRamRange6 = 25769803769L;
long errorRamRange7 = 34359738368L;
long result = decimal;
if((result >= errorRamRange0) && (result <= errorRamRange1))
{
chipRangeFall = "1";
}
else if ((result >= errorRamRange2) && (result <= errorRamRange5))
{
chipRangeFall = "2";
}
else if ((result >= errorRamRange4) && (result <= errorRamRange3))
{
chipRangeFall = "3";
}
else if ((result >= errorRamRange6) && (result <= errorRamRange7))
{
chipRangeFall = "4";
}
else
{
System.out.println("ram chip does not exist");
}
return chipRangeFall;
}
public static void main(String[] args) throws IOException
{
readFile(convertFile);
readingLine(convertFile);
}
}
答案 0 :(得分:3)
您希望对%s
参数String
进行recordingLine
转换,而不是%g
答案 1 :(得分:1)
recordLine预计会浮动,但发现是字符串。