Number Format Exception when converting a string - java

The following code won't run and keeps throwing a Number Format Exception on the line the error is "Exception in thread "main" java.lang.NumberFormatException: For input string: "SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652" ""
intAlt = Integer.parseInt(strAlt);
I'm not sure why this is happening and would appreciate any advice on the matter
private int getPos(String strAlt)
{
int intAlt;
int intPos =0;
intAlt = Integer.parseInt(strAlt);
switch (intAlt)
{
case 3:
intPos = 4;
break;
case 6:
intPos = 9;
break;
case 9:
intPos = 17;
break;
case 12:
intPos = 25;
break;
case 18:
intPos = 33;
break;
case 24:
intPos = 41;
break;
case 30:
intPos = 49;
break;
case 34:
intPos = 56;
break;
case 39:
intPos = 63;
break;
}
return intPos;
}

If you pass "SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652" into getPos(String strAlt), it will execute:
intAlt = Integer.parseInt(strAlt);
Of course it will throw exception.
See this doc

Related

Generator printing null before the intended output

I've created a random word generator and it seems to work pretty well except for the fact that before the desired output, in the same line, it prints "null".
Here's my code:
import java.util.Random;
public class wordGenerator {
private static String r,s;
public static void randChar(int x) {
Random rand = new Random();
x = rand.nextInt((26 - 1) + 1) + 1;
switch(x) {
case 1: r = "a"; break;
case 2: r = "b"; break;
case 3: r = "c"; break;
case 4: r = "d"; break;
case 5: r = "e"; break;
case 6: r = "f"; break;
case 7: r = "g"; break;
case 8: r = "h"; break;
case 9: r = "i"; break;
case 10: r = "j"; break;
case 11: r = "k"; break;
case 12: r = "l"; break;
case 13: r = "m"; break;
case 14: r = "n"; break;
case 15: r = "o"; break;
case 16: r = "p"; break;
case 17: r = "q"; break;
case 18: r = "r"; break;
case 19: r = "s"; break;
case 20: r = "t"; break;
case 21: r = "u"; break;
case 22: r = "v"; break;
case 23: r = "w"; break;
case 24: r = "x"; break;
case 25: r = "y"; break;
case 26: r = "z"; break;
default: r = "|null|";
}
if(x != 1 && x != 5 && x != 9 && x != 15 && x != 21) {
int h = rand.nextInt(4 - 0);
if(h == 2) {
int k = rand.nextInt(6 - 0);
switch(k) {
case 1: r = "a"; break;
case 2: r = "e"; break;
case 3: r = "i"; break;
case 4: r = "o"; break;
case 5: r = "u"; break;
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand = new Random();
int y = rand.nextInt((10 - 4) + 1) + 4;
for(int z = 1; z < y; z++) {
randChar(0);
s = s + r;
}
System.out.println(s);
}
To me it seems fine, but when I think it should output something like
opfruvme
it prints
nullopfruvme
What's the problem?
You didn't initialize s.
private static String r,s="";
When you start your application in the loop the statement s = s + r; takes null value before concatenating with r in the first cycle. Initializing variable with empty string will give you result that you have been expected.
You didn't initialize s, so then the first time the s = s + r; line executes the current value of s (null) will be coerced to a String, so you will assign "null" + r to s.

variable might not have been initialised

I'm a relatively new student learning Java programming, but I'd like to ask for some help. The error I'm receiving in my code is stating "variable romanNumeral might not have been initialised."
The intent for this program is for a user to enter a number from 1-39 and then have the appropriate roman numeral value displayed to the user via a dialog box. The code is not complete yet, as I've yet to find a solution to this problem due to the application not letting me compile my code.
Here is the code:
public class exercise4 extends Actor
{
int userNum;
public void act()
{
intInput(userNum);
}
public String intInput(int userNum)
{
String userInput;
String romanNumeral;
userInput = JOptionPane.showInputDialog("Please enter a number to be converted into Roman Numerals.");
userNum = Integer.parseInt(userInput);
switch(userNum)
{
case 1: romanNumeral = "I";
break;
case 2: romanNumeral = "II";
break;
case 3: romanNumeral = "III";
break;
case 4: romanNumeral = "IV";
break;
case 5: romanNumeral = "V";
break;
case 6: romanNumeral = "VI";
break;
case 7: romanNumeral = "VII";
break;
case 8: romanNumeral = "VIII";
break;
case 9: romanNumeral = "IX";
break;
case 10: romanNumeral = "X";
break;
case 11: romanNumeral = "XI";
break;
case 12: romanNumeral = "XII";
break;
case 13: romanNumeral = "XIII";
break;
case 14: romanNumeral = "XIV";
break;
case 15: romanNumeral = "XV";
break;
case 16: romanNumeral = "XVI";
break;
case 17: romanNumeral = "XVII";
break;
case 18: romanNumeral = "XVIII";
break;
case 19: romanNumeral = "XIX";
break;
case 20: romanNumeral = "XX";
break;
case 21: romanNumeral = "XXI";
break;
case 22: romanNumeral = "XXII";
break;
case 23: romanNumeral = "XXIII";
break;
case 24: romanNumeral = "XXIV";
break;
case 25: romanNumeral = "XXV";
break;
case 26: romanNumeral = "XXVI";
break;
case 27: romanNumeral = "XXVII";
break;
case 28: romanNumeral = "XXVIII";
break;
case 29: romanNumeral = "XXIX";
break;
case 30: romanNumeral = "XXX";
break;
case 31: romanNumeral = "XXXI";
break;
case 32: romanNumeral = "XXXII";
break;
case 33: romanNumeral = "XXXIII";
break;
case 34: romanNumeral = "XXXIV";
break;
case 35: romanNumeral = "XXXV";
break;
case 36: romanNumeral = "XXXVI";
break;
case 37: romanNumeral = "XXXVII";
break;
case 38: romanNumeral = "XXXVIII";
break;
case 39: romanNumeral = "XXXIX";
break;
}
return romanNumeral;
}
}
Consider how the code would behave if userNum has a value of 40. The switch statement doesn't have a case that matches such a value, so it would do nothing. Which is what the compiler is complaining about: the variable romanNumeral is not initialized when it's declared, and might not be even after the switch - thus: "variable romanNumeral might not have been initialised."
Two simple fixes: (A) initialize at declaration, e.g. String romanNumeral = "?", or (B) add a default part to the switch, as:
switch(userNum)
{
// other cases first
default: romanNumeral = "?";
}
use default in your switch case. in java you must have initialize a variable before using it. in your code if there is a value where no case matches then the variable will not be initialized.
Add in a default case to your switch statement setting it to some error value. You are getting that warning because it is possible your switch matches none of that and romanNumeral will never get set before it's returned.
String romanNumeral makes a reference to a memory location, but doesn't initialize it (doesn't give it a value). Because you can provide a value of usernum that doesn't cause a value to be set for romanNumeral, you're getting an error. To avoid this, you can add a default case.
In Java, variables defined in some method are not automatically initialized.
Here you have two options:
1. Initialize it using
String romanNumeral = null (or something);
2. Use default in switch
default:
romanNumeral = null (or something);
The error just means that the variable still has no memory allocated in it. so, what you will do to remove the error is just to give it an initial value.This will do :
String romanNumeral = "";
It is important to initialize the variable, object instances or any data structure you use. Sometimes it gives the null error but sometimes it does not even give an error and can give wrong values.
Your question have been answered above but I would like to suggest a modification. Following this table, you can make a HashMap of the roman numbers:
Decimal value (v) Roman numeral (n)
1 I
4 IV
5 V
9 IX
10 X
40 XL
50 L
90 XC
100 C
400 CD
500 D
900 CM
1000 M
Using the hashmap you can calculate the roman number for particular integer.Check the code just to give you an idea to get started:
public static void main(String []args){
int c = 39;int temp =0;
String roman = "";
if(c<40 && C>10)
{
temp = c/10;
c = c%10;
for(int i=0;i<temp;i++)
{
roman = roman+map.get(10);
}
}
if(c<10 && c>5)
{
if(c==9)
{
roman = roman+map.get(9);
}else{
temp = c/5;
c = c%5;
if(temp==1)
roamn += map.get(5);
for(int i=0;i<c;i++)
{
roman = roman+map.get(1);
}
//again you will have to check a case for four the way I did for 9
}
}
}
System.out.println(roman);
}

NullPointerException - android.view.Window.findViewById(int) - Why? [duplicate]

This question already has answers here:
Android : getting NullPointerException for ImageView imag = (ImageView) findViewById(R.id.image)
(5 answers)
Closed 7 years ago.
I have had multiple problems with android app dev kit and I can't seem to fix it. I tried running my code and this is what shows up in logcat
Here is my main activity:
public class MainActivity extends AppCompatActivity {
EditText input = (EditText) findViewById(R.id.input);
TextView output = (TextView) findViewById(R.id.output);
ArrayList <String> charzart = new ArrayList<String>();
ArrayList <String> color = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Button converter = (Button) findViewById(R.id.button);
int begin = 0;
int total = 16777216;
String text = input.getText().toString();
ArrayList<Integer> hexa = new ArrayList<Integer>();
//charzart array
int b = text.length();
for (int i = 1; i <= b; i++) {
charzart.add(text.substring(i - 1, i));
}
//hexa array
int a = text.length() - 1;
for (int z = 0; z < charzart.size(); z++) {
if (charzart.get(z).matches(" ")) {
a--;
}
}
int numb = total / a;
for (int x = 0; x <= 16777216; x = x + numb) {
hexa.add(x);
}
//changing into hexadecimal numbers
for (int y = 0; y < hexa.size(); y++) {
int cvalue = hexa.get(y);
int one, two, three, four, five, six;
one = cvalue / 1048570;
cvalue = cvalue - one * 1048570;
two = cvalue / 65356;
cvalue = cvalue - two * 65356;
three = cvalue / 4096;
cvalue = cvalue - three * 4096;
four = cvalue / 256;
cvalue = cvalue - four * 256;
five = cvalue / 16;
cvalue = cvalue - five * 16;
six = cvalue;
if (one != 16) {
String let = "";
String fin = "#";
switch (one) {
case 0:
let = "0";
break;
case 1:
let = "1";
break;
case 2:
let = "2";
break;
case 3:
let = "3";
break;
case 4:
let = "4";
break;
case 5:
let = "5";
break;
case 6:
let = "6";
break;
case 7:
let = "7";
break;
case 8:
let = "8";
break;
case 9:
let = "9";
break;
case 10:
let = "A";
break;
case 11:
let = "B";
break;
case 12:
let = "C";
break;
case 13:
let = "D";
break;
case 14:
let = "E";
break;
case 15:
let = "F";
break;
default:
let = "x";
}
fin += let;
let = "";
switch (two) {
case 0:
let = "0";
break;
case 1:
let = "1";
break;
case 2:
let = "2";
break;
case 3:
let = "3";
break;
case 4:
let = "4";
break;
case 5:
let = "5";
break;
case 6:
let = "6";
break;
case 7:
let = "7";
break;
case 8:
let = "8";
break;
case 9:
let = "9";
break;
case 10:
let = "A";
break;
case 11:
let = "B";
break;
case 12:
let = "C";
break;
case 13:
let = "D";
break;
case 14:
let = "E";
break;
case 15:
let = "F";
break;
default:
let = "x";
}
fin += let;
let = "";
switch (three) {
case 0:
let = "0";
break;
case 1:
let = "1";
break;
case 2:
let = "2";
break;
case 3:
let = "3";
break;
case 4:
let = "4";
break;
case 5:
let = "5";
break;
case 6:
let = "6";
break;
case 7:
let = "7";
break;
case 8:
let = "8";
break;
case 9:
let = "9";
break;
case 10:
let = "A";
break;
case 11:
let = "B";
break;
case 12:
let = "C";
break;
case 13:
let = "D";
break;
case 14:
let = "E";
break;
case 15:
let = "F";
break;
default:
let = "x";
}
fin += let;
let = "";
switch (four) {
case 0:
let = "0";
break;
case 1:
let = "1";
break;
case 2:
let = "2";
break;
case 3:
let = "3";
break;
case 4:
let = "4";
break;
case 5:
let = "5";
break;
case 6:
let = "6";
break;
case 7:
let = "7";
break;
case 8:
let = "8";
break;
case 9:
let = "9";
break;
case 10:
let = "A";
break;
case 11:
let = "B";
break;
case 12:
let = "C";
break;
case 13:
let = "D";
break;
case 14:
let = "E";
break;
case 15:
let = "F";
break;
default:
let = "x";
}
fin += let;
let = "";
switch (five) {
case 0:
let = "0";
break;
case 1:
let = "1";
break;
case 2:
let = "2";
break;
case 3:
let = "3";
break;
case 4:
let = "4";
break;
case 5:
let = "5";
break;
case 6:
let = "6";
break;
case 7:
let = "7";
break;
case 8:
let = "8";
break;
case 9:
let = "9";
break;
case 10:
let = "A";
break;
case 11:
let = "B";
break;
case 12:
let = "C";
break;
case 13:
let = "D";
break;
case 14:
let = "E";
break;
case 15:
let = "F";
break;
default:
let = "x";
}
fin += let;
let = "";
switch (six) {
case 0:
let = "0";
break;
case 1:
let = "1";
break;
case 2:
let = "2";
break;
case 3:
let = "3";
break;
case 4:
let = "4";
break;
case 5:
let = "5";
break;
case 6:
let = "6";
break;
case 7:
let = "7";
break;
case 8:
let = "8";
break;
case 9:
let = "9";
break;
case 10:
let = "A";
break;
case 11:
let = "B";
break;
case 12:
let = "C";
break;
case 13:
let = "D";
break;
case 14:
let = "E";
break;
case 15:
let = "F";
break;
default:
let = "x";
}
fin += let;
let = "";
color.add(fin);
fin = "#";
} else {
color.add("#FFFFFF");
}
}
//BUTTON
converter.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
String end = "";
for (int p = 0; p < charzart.size(); p++) {
if (charzart.get(p).equals(" ")) {
}
else{
Spannable colorSpan = new SpannableString(charzart.get(p));
int fake = Integer.parseInt(color.get(p));
colorSpan.setSpan(new ForegroundColorSpan(fake), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
end += colorSpan;
}
}
output.setText(end);
}
}
);
}
Here's my layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.hitchhikingapps.rainbowz.MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert"
android:id="#+id/button"
android:layout_marginTop="124dp"
android:layout_below="#+id/input"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/input"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="Insert Text here" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/output"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="78dp" />
</RelativeLayout>
And this is an error which I got it:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hitchhikingapps.rainbowz/com.hitchhikingapps.rainbowz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2090)
at com.hitchhikingapps.rainbowz.MainActivity.<init>(MainActivity.java:22)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Sorry for the great block of code, but I really don't understand what the problem is. Any help would be appreciate
The problem here is that you are initializing your views as global variables:
EditText input = (EditText) findViewById(R.id.input);
TextView output = (TextView) findViewById(R.id.output);
The current context of your views here is Window and not R.layout.activity_main.
If you want to use input and output as global variables, you have only to declare them:
EditText input;
TextView output;
and initialize them in onCreate() after setContentView():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
output = (TextView) findViewById(R.id.output);
}
The NPE is because you're calling findViewById() before onCreate() in the activity lifecycle before the activity has a Window. Move the findViewById() calls from member variable initialization to onCreate() and after setContentView() so that a non-null value can be returned.

Java randInt switch function issue

I am kind of relatively new to java and am working on a project for class. This is a portion of the system and i have been getting errors in this piece.
public class NumberTest
{
private static void main( String[] args )
{
}
static private char GenerateNumber()
{
int gennumber = 0;
while(gennumber < 3)
{
char returnChar = ' ';
switch(randInt(0,9))
{
case 0:
returnChar = '0';
break;
case 1:
returnChar = '1';
break;
case 2:
returnChar = '2';
break;
case 3:
returnChar = '3';
break;
case 4:
returnChar = '4';
break;
case 5:
returnChar = '5';
break;
case 6:
returnChar = '6';
break;
case 7:
returnChar = '7';
break;
case 8:
returnChar = '8';
break;
case 9:
returnChar = '9';
break;
default:
System.out.println("Error Random int outside expected values");
}
return returnChar;
System.out.println(GenerateNumber());
gennumber++;
}
}
}
The error I am receiving is:
Test.Again.java:12: error: cannot find symbol
switch(randInt(0,9)
symbol: method randInt(int,int)
location: class NumberTest
You are trying to call undeclared function randInt. It means you must first write this function in your class and then use it.
However you may use java.util.Random:
Random rand = new Random();
switch(rand.nextInt() % 10)
...

Filtering of ipv4 addresses using array in java

I have to filter IP address of Ipv4(32 bit) , but when i use array index up to 32 bit {array[32 bit]} it give error. So i divided the ip to three parts as;
1-one part contain index value of 25 bit { array[25 bit]}
2- second part contain value of 5 bit and this value is decoded using 5 to 32 decoder.
3-The third part of two bit indicate 4 different array in which 5 bit decoded value is stored on 25 bit address location.
It gives an error on fourth short array.
when i reduce address to 24 bit and decode 5 bit to store in 8 different short array, here 3 bits select array out of 8, then it also give an error on eighth short array,
"Exception in thread "main" java.lang.OutOfMemoryError: Java heap space"
How can this problem is resolved?
Is there any other data structure like linkedlist, tree etc that can solve this problem?
import java.util.Scanner;
class IPAddress3 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter IP address to search from list:");
String ipAddress=input.nextLine();
//String ipAddress="171.255.100.23";
String[] octets = ipAddress.split("\\.");
StringBuilder sb = new StringBuilder();
for (String octet : octets) {
int number = Integer.parseInt(octet);
if(number>255)
{
System.out.println("Error:Invalid Ip address,\n" +
"Each octet should be less or equal to 255\n");
System.exit(0);
}
else{
String binaryPart = Integer.toBinaryString(number);
if (binaryPart.length() < 8) {
for (int i = binaryPart.length(); i < 8; i++) {
binaryPart = "0" + binaryPart;
}
}
sb.append(binaryPart);
}
}
String binaryForm = sb.toString();
System.out.println(binaryForm);
System.out.println("2 MSB: " + binaryForm.substring(0, 2));
System.out.println("5 bits: " + binaryForm.substring(2, 7));
System.out.println("25 LSB: " + binaryForm.substring(7));
String MSB2Bit=binaryForm.substring(0, 2);
String fivebits= binaryForm.substring(2, 7);
String twenty5bits=binaryForm.substring(8);
byte msb2Bit=Byte.parseByte(MSB2Bit);
byte data=Byte.parseByte(fivebits,2);
int address=Integer.parseInt(twenty5bits,2);
System.out.println("Data:\t"+data);
System.out.println("Address: "+address);
short[] Array0=new short[33554432];
short[] Array1=new short[33554432];
short[] Array2=new short[33554432];
short[] Array3=new short[33554432];//error in this line
/*
short[] Array0=new short[16777216];
short[] Array1=new short[16777216];
short[] Array2=new short[16777216];
short[] Array3=new short[16777216];
short[] Array4=new short[16777216];
short[] Array5=new short[16777216];
short[] Array6=new short[16777216];
short[] Array7=new short[16777210];// error here
/*
Array0[1677721]=127;
Array1[1677721]=127;
Array2[1677721]=127;
int thirty2BitWord = 0 ;// Array0[ad]=Array0[ad] | Arra0[ad];
int filterWord = 8;
switch(data)
{
case 0:
thirty2BitWord=1;
break;
case 1:
thirty2BitWord=2;
break;
case 2:
thirty2BitWord=4;
break;
case 3:
thirty2BitWord=8;
break;
case 4:
thirty2BitWord=16;
break;
case 5:
thirty2BitWord=32;
break;
case 6:
thirty2BitWord=64;
break;
case 7:
thirty2BitWord=128;
break;
case 8:
thirty2BitWord=256;
break;
case 9:
thirty2BitWord=512;
break;
case 10:
thirty2BitWord=1024;
break;
case 11:
thirty2BitWord=2048;
break;
case 12:
thirty2BitWord=4096;
break;
case 13:
thirty2BitWord=8192;
break;
case 14:
thirty2BitWord=16384;
break;
case 15:
thirty2BitWord=32768;
break;
case 16:
thirty2BitWord=65536;
break;
case 17:
thirty2BitWord=131072;
break;
case 18:
thirty2BitWord=262144;
break;
case 19:
thirty2BitWord=524288;
break;
case 20:
thirty2BitWord=1048576;
break;
case 21:
thirty2BitWord=2097152;
break;
case 22:
thirty2BitWord=4194304;
break;
case 23:
thirty2BitWord=8388608;
break;
case 24:
thirty2BitWord=16777216;
break;
case 25:
thirty2BitWord=33554432;
break;
case 26:
thirty2BitWord=67108864;
break;
case 27:
thirty2BitWord=134217728;
break;
case 28:
thirty2BitWord=268435456;
break;
case 29:
thirty2BitWord=536870912;
break;
case 30:
thirty2BitWord=1073741824;
break;
case 31:
thirty2BitWord=2147483647;
break;
}
if(msb2Bit==0)
{
filterWord=thirty2BitWord & Array0[address];
}
else if(msb2Bit==1)
{
filterWord=thirty2BitWord & Array1[address];
}
else if(msb2Bit==2)
{
filterWord=thirty2BitWord & Array2[address];
}
/* else
{
filterWord=thirty2BitWord & Array3[address];
}*/
byte filterbit;
if(filterWord==0)
{
filterbit=0;
System.out.println("No such address is found in blacklist, So Allow\n");
}
else
{
filterbit=1;
System.out.println("IP match, So block this address\n");
}
}
}
For the short run you can fix the heap size error by increasing the memory:
How to increase application heap size in Eclipse?
I havn't checked you code too much as this error is simply the VM running out of memory. There might be a better way doing this but if the program just needs more RAM this should do the trick!

Categories