0

i have a program that converts a text string into a hash sequence using parseInt(). but parseInt() keeps throwing a NumberFormatException.

any ideas why? is it becuase i'm using <= instead of = in my for loop?

EDIT: ingnore r. it's not being used. sorry.

package test;

import java.util.Random;
import java.util.Scanner;

public class minecraft_check {

    public static void main(String[] args)
    {
        String yn =  null;
        Random rand = new Random();
        String r = null;
        int hash = 0;
        do
        {
            do
            {
            System.out.print("Enter the Seed to be converted (max 30 chars.): ");
            r = new Scanner(System.in).nextLine();
            }
            while(r.length() > 31);
            hash = getHashCode(r, rand);
            System.out.println("\nYour Hash code for " + r + " is: " + hash + "\n\n");
            System.out.println("Again? (y/n)");
            yn = new Scanner(System.in).nextLine();
        }while(yn != "n" && yn != "N");
        System.exit(0);
    }


    public static int getHashCode(String seed, Random rand)
    {
        char[] split = seed.toCharArray();
        int generatornum = 0, r = 0;
        for(int i = 0; i <= seed.length();)
        {
             r = rand.nextInt(256);
            if(!(r == 0 || r == 256))
            {
                generatornum+=(Integer.parseInt(Integer.toString((int)split[i]), 256)) * Math.pow(31,seed.length()-i);
                i++;
            }else continue;
        }
        return generatornum;
    }
}

Edited by TTTHXC

3
Contributors
15
Replies
16
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

What is the contents of th String you are trying to parse?
How is it getting non-numeric data?
If you can't see what the value being parsed is, assign it to a String first and print the value of the String so you can see it.

Does the full text of the error message show you the invalid String?

0

here is the layout

seed is the base string, and is got from imput from the main function.
seed s split into single chars. to get the proper hash, i have to get the value of each character seperately.

problem is though, parseInt does not accept Character. so i have to reinterpret the character as a String that contains an integral value by dynamically converting the character to an integer.

then finaly i can parse the string into a number based on the number of ascii characters and complete the calculation.

eclipse is showing that line 40, is causing a problem, along with line 20 of my program and line 426 of integer.class

Edited by TTTHXC

0

What value are you trying to get from the single char value: split[i]? Can you give some examples: '?' to int value

What is the full text of the compiler's error message?

Edited by NormR1

0

eclipse throws: Exception in thread "main" java.lang.NumberFormatException: radix 128 greater than Character.MAX_RADIX

0

That error seems clear. Where is the 128 coming from? I see 256 in the posted code.
What about my other question?

Edited by NormR1

0

i tried a lower number to see if it would correct the problem earlier.

here is the formula:
s[i]31^(n-i) +s[i]31^(n-i) + s[i]*31^(n-i)... until i = n

where s[i] is the character and n is the length of the string.

Edited by TTTHXC

0

sure. say split[1] = "A"

A -> 65 decimal

therfore: 65 *(30^1 * 1) = 65 * 30 = 1950

thefore the hash is 1950.

Edited by TTTHXC

0

What was the purpose of this expression in your code:
Integer.parseInt(Integer.toString((int)split[i]), 256)
You wanted to use the value of split[i] to get an int.

Edited by NormR1

0

in order to get the value of the character. 256 is the maximum number of characters in the ASCII table.

0

Can you show some examples? What values did you want that expression to return? What were the minimum and max values you wanted to get?

0

assumming that the user has entered character 0xFF (in hex. idk what it's represenation is) 30 times then.

255*(30^30-1) +.... (the eqaution is ver long) = ~5.5 * 10^46

the lowest number is 1 becasue entering 0x01 would result in:
1*(30^1-1) = 1 * 30^0 = 1

Edited by TTTHXC

0

How does a user enter a character with the value: 0xFF?

You are not answering my question. what value did you want to get from split[i] using the expression I posted?
I'm not asking what you are going to do with the value AFTER you get it.

0

if a user on the keyboard press ALT+0+2+5+5 it will display 0xFF

if a user enters a character, say Q, it needs to read 81 then calculate.

i solved my problem now. i just rewrote the function.

Edited by TTTHXC

0

You don't say how you re-wrote it, but for future reference, a Java char is a numeric value. If you get a single char (eg by converting a String to a char array) then you can just use that value in a numeric expression without any further conversion, eg

      String str = "QRS";
      char[] chars = str.toCharArray();
      System.out.println(chars[0] * 2); // prints 162 ('Q' == 81)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.