Compilation error in this code - java

Compilation error in this code , how can I fix this java code?
anyone know how to fix this? and the label284; is giving some problem.
Pastebin : http://pastebin.com/gWKwnqg5
Image : http://i.imgur.com/OwbdR.png
private List<int[]> getDataByAverage()
{
int i = this.money;
Object localObject1 = new ArrayList();
if (this.num != 1)
{
for (int j = 0; j < this.num; j++)
((List)localObject1).add(new int[2]);
i /= this.num;
j = 0;
int k = 0;
while (k < this.num)
{
Object localObject2;
if (k + 1 != this.num)
{
int n;
if (10.0D * Math.random() <= 5.0D)
n = 0;
else
n = 1;
int m = (int)(Math.round(Math.random() * i) / 2L);
localObject2 = (int[])((List)localObject1).get(k);
if (n == 0)
m = i - m;
else
m = i + m;
localObject2[0] = m;
j += ((int[])localObject1.get(k))[0];
}
else
{
localObject2 = new BigDecimal(String.valueOf(this.money));
BigDecimal localBigDecimal = new BigDecimal(String.valueOf(j));
((int[])localObject1.get(k))[0] = ((BigDecimal)localObject2).subtract(localBigDecimal).intValue();
}
if (((int[])localObject1.get(k))[0] >= 0)
{
k++;
continue;
}
localObject1 = getDataByAverage();
break label284;
}
localObject1 = localObject1;
}
else
{
int[] arrayOfInt = new int[2];
arrayOfInt[0] = this.money;
((List)localObject1).add(arrayOfInt);
localObject1 = localObject1;
}
label284: return (List<int[]>)(List<int[]>)localObject1;
}

I guess labeled break is used to get out of multiple for or while loops. And you will have to declare the label above where you are using it.
you can check here
You will have to move label284: before it is used.
Might well be a method to declare a label which i am not aware of
Edit: Here's the method, put braces across the whole if (this.num != 1) else { } routine. Then define label284: before it.
Apparently the break label will goto end of statement. For more details check here

try:
private List<int[]> getDataByAverage()
{
int i = this.money;
Object localObject1 = new ArrayList();
if (this.num != 1)
{
for (int j = 0; j < this.num; j++)
((List)localObject1).add(new int[2]);
i /= this.num;
j = 0;
int k = 0;
Object localObject2;
if (k + 1 != this.num)
{
int n;
if (10.0D * Math.random() <= 5.0D)
n = 0;
else
n = 1;
int m = (int)(Math.round(Math.random() * i) / 2L);
localObject2 = (int[])((List)localObject1).get(k);
if (n == 0)
m = i - m;
else
m = i + m;
localObject2= m;
j += ((int[])((List<int[]>) localObject1).get(k))[0];
}
else
{
localObject2 = new BigDecimal(String.valueOf(this.money));
BigDecimal localBigDecimal = new BigDecimal(String.valueOf(j));
((int[])((List<int[]>) localObject1).get(k))[0] = ((BigDecimal)localObject2).subtract(localBigDecimal).intValue();
}
if (((int[])((List<int[]>) localObject1).get(k))[0] >= 0)
{
k++;
}
localObject1 = getDataByAverage();
localObject1 = localObject1;
}
else
{
int[] arrayOfInt = new int[2];
arrayOfInt[0] = this.money;
((List)localObject1).add(arrayOfInt);
localObject1 = localObject1;
}
return (List<int[]>)(List<int[]>)localObject1;
}

Declare localObject1 as a List instead of an Object. That should fix this error.

Related

How do I fix the resource leak in my RSA algorithm (java)?

I am attempting to make an RSA algorithm and i'm not sure how to close my scanner. I have attempted to use .close(); for instance trying to place it at the end of my RSA class but to no avail. Most likley I am just not putting it in the correct place or because i keep getting the following error in Visual Studio on my line 19:
Help much appreciated.
import java.util.*;
public class RSA {
static int gcd(int m, int n) {
while (n != 0) {
int r = m % n;
m = n;
n = r;
}
return m;
}
public static void main(String args[]) {
int p = 0, q = 0, n = 0, e = 0, d = 0, phi = 0;
int nummes[] = new int[100];
int encrypted[] = new int[100];
int decrypted[] = new int[100];
int i = 0, j = 0, nofelem = 0;
Scanner sc = new Scanner(System.in);
String message;
System.out.println("Enter the Message to be encrypted:");
message = sc.nextLine();
System.out.println("Enter value of p and q\n");
p = sc.nextInt();
q = sc.nextInt();
n = p * q;
phi = (p - 1) * (q - 1);
for (i = 2; i < phi; i++)
if (gcd(i, phi) == 1) break;
e = i;
for (i = 2; i < phi; i++)
if ((e * i - 1) % phi == 0) break;
d = i;
for (i = 0; i < message.length(); i++) {
char c = message.charAt(i);
nummes[i] = c - 96;
}
nofelem = message.length();
for (i = 0; i < nofelem; i++) {
encrypted[i] = 1;
for (j = 0; j < e; j++)
encrypted[i] = (encrypted[i] * nummes[i]) % n;
}
System.out.println("\n Encrypted message\n");
for (i = 0; i < nofelem; i++) {
System.out.print(encrypted[i]);
System.out.print((char) (encrypted[i] + 96));
}
for (i = 0; i < nofelem; i++) {
decrypted[i] = 1;
for (j = 0; j < d; j++)
decrypted[i] = (decrypted[i] * encrypted[i]) % n;
}
System.out.println("\n Decrypted message\n ");
for (i = 0; i < nofelem; i++)
System.out.print((char) (decrypted[i] + 96));
return;
}
}

How can I modify the boyer moore algorithm so that it runs in 0(n+m) time?

So I'm using the Boyer-Moore function and trying to integrate the failure function from the KMP and I don't know how to do that per se. I added the calling method to the failure function but I don't know how to make use of it. The two methods are given below.
public static int findBoyerMoore(char[] text, char[] pattern) {
int n = text.length;
int m = pattern.length;
int[] fail = computeFailKMP(pattern); ---> I did this but how can I use fail?
if(m == 0){
return 0;
}
Map<Character, Integer> last = new HashMap<>();
for(int i = 0; i< n; i++) {
last.put(text[i], -1);
}
for(int k = 0; k < m; k++) {
last.put(pattern[k], k);
}
int i = m - 1;
int k = m - 1;
while(i < n) {
if(text[i] == pattern[k]) {
if(k == 0) {
return i;
}
i--;
k--;
} else {
i+= m - Math.min(k, 1+ last.get(text[i]));
k = m-1;
}
}
return -1;
}
public static int[] computeFailKMP(char[] pattern) {
int m = pattern.length;
int[] fail = new int[m];
int j = 0;
int k = 0;
while(j < m) {
if(pattern[j] == pattern[k]) {
fail[j] = k + 1;
j++;
k++;
}
else if(k > 0) {
k = fail[k - 1];
}
else {
j++;
}
}
return fail;
}

Getting NZEC in Java for PARTSUM on SPOJ

Array T[] has been used to store the sum of all possible contiguous combinations. And at each step, I am checking whether it is atleast k or not and that value is stored in the variable val. Below is the Java Code:
import java.util.Arrays;
import java.util.Scanner;
class Codechef {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
long t = sc.nextLong();
while (t > 0) {
int n = sc.nextInt();
long k = sc.nextLong();
long p = sc.nextLong();
long a[] = new long[n];
long T[] = new long[((n * (n + 1)) / 2)];
Arrays.fill(T, -1);
long sum = 0, x = 0, val = p, f = 0;
int i = 0;
for (i = 0; i < n; i++) {
a[i] = sc.nextLong();
sum = sum + a[i];
x = sum % p;
T[i] = x;
if (T[i] == k)
f = 1;
else if (T[i] > k && T[i] < val)
val = T[i];
}
if (f != 1) {
for (int j = i - 1; j > 0; j--) {
for (int m = 0; m < j; m++) {
x = T[j] - T[m];
if (x < 0)
x += p;
T[i] = x;
if (T[i] == k) {
f = 1;
break;
}
else if (T[i] > k && T[i] < val)
val = T[i];
i++;
}
if (f == 1)
break;
}
}
if (f == 1)
System.out.println(k);
else
System.out.println(val);
t--;
}
}
}
The above is the code for PARTSUM- Partial Sums on SPOJ. What could be the possible error?
Previously I was getting Runtime error NZEC on submit. Now that has been corrected and now I am getting TLE. Any optimization?

Exception in thread "main" java.util.NoSuchElementException on ideone

This is my first code with java .when I tested it on Ideone. It showed:
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 Hw2_p4.main(Main.java:22)
I searched for answers but I didn't get the problem cause or how to fix it.
the code runs on eclipse normally
Here is the code
import java.util.Scanner;
class Hw2_p4 {
static void swap(String[] A, int a, int b) {
String temp = A[a];
A[a] = A[b];
A[b] = temp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
int i, j, k, l, counter = 0;
String[] name = new String[16];
String[][] notalong = new String[120][2];
String[] temp = new String[120];
boolean[][] A = new boolean[120][2];
for (i = 0; i < n; i++) {
name[i] = sc.next();
}
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
notalong[i][j] = sc.next();
}
}
int flag = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
flag = 0;
for (k = i + 1; k < m; k++) {
for (l = 0; l < 2; l++) {
if (notalong[i][j].compareToIgnoreCase(notalong[k][l]) == 0 && A[i][j] == false && A[k][l] == false) {
A[k][l] = true;
flag = 1;
}
}
}
if (flag == 1) {
A[i][j] = true;
counter++;
} else if (flag == 0 && A[i][0] == false && A[i][1] == false) {
A[i][j] = true;
counter++;
}
}
}
System.out.println(n - counter);
int x = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
if (A[i][j] == false) {
temp[x++] = notalong[i][j];
A[i][j] = true;
for (k = i + 1; k < m; k++) {
for (l = 0; l < 2; l++) {
if (notalong[i][j].compareToIgnoreCase(notalong[k][l]) == 0 && A[k][l] == false) {
A[k][l] = true;
}
}
}
}
}
}
//compare not along with names
int found = 1;
for (i = 0; i < n; i++) {
found = 0;
for (j = 0; j < m; j++) {
for (k = 0; k < 2; k++) {
if (name[i].compareToIgnoreCase(notalong[j][k]) == 0) {
found = 1;
}
}
}
if (found == 0) {
temp[x++] = name[i];
}
}
//sorting lexicographically
boolean swapp = true;
for (i = 0; i < x && swapp; i++) {
swapp = false;
for (j = 0; j < x - i - 1; j++) {
if (temp[j].compareToIgnoreCase(temp[j + 1]) > 0) {
swap(temp, j, j + 1);
swapp = true;
}
}
}
for (i = 0; i < x; i++) {
System.out.println(temp[i]);
}
}
}
Ideone is not interactive. You have to click on Specify input and enter all of your input in there before you run the application. What you are seeing is an exception because System.in has an "end of file" status.
You ought to protect sc.next() with cs.hasNext():
while (!sc.hasNext()) {
Thread.sleep(100);
}
int n = sc.nextInt();
However since you read the input in many location throughout your code try using Scanner.nextLine() preferably printing to the console, what is the input you are expecting next. nextLine() will read all the input until the user presses ENTER, which is a nicer 'user journey' than simply constantly reading keyboard and passing it to System.in.
System.out.print("Please enter your favourite number : "); //NB print and space is last
String input = sc.nextLine();
int favNumber = Double.parseDouble(input);
the way that Ideone gives input to your code is different from the way your IDE(or you)gives input.
so you should probably figure out how does Ideone gives input and then fix your code to that way of inputs.

Sorting an array of String objects on J2ME platform

What is the best(fastest) way to sort an array of Strings (using Java 1.3).
You can use this code for sort the string values,
public Vector sort(String[] e) {
Vector v = new Vector();
for(int count = 0; count < e.length; count++) {
String s = e[count];
int i = 0;
for (i = 0; i < v.size(); i++) {
int c = s.compareTo((String) v.elementAt(i));
if (c < 0) {
v.insertElementAt(s, i);
break;
} else if (c == 0) {
break;
}
}
if (i >= v.size()) {
v.addElement(s);
}
}
return v;
}
Also see this sample code for using bubble sort,
static void bubbleSort(String[] p_array) throws Exception {
boolean anyCellSorted;
int length = p_array.length;
String tmp;
for (int i = length; --i >= 0;) {
anyCellSorted = false;
for (int j = 0; j < i; j++) {
if (p_array[j].compareTo(p_array[j + 1]) > 0) {
tmp = p_array[j];
p_array[j] = p_array[j + 1];
p_array[j + 1] = tmp;
anyCellSorted = true;
}
}
if (anyCellSorted == false) {
return;
}
}
}
Use java.util.Arrays.sort.
If it's not possible for some reason due to limitations of the platform, you can get ideas from its source.

Categories

Resources