I tried a problem which worked fine on C++; here is the C++ implementation.
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
int t;
char str[1000100];
char num[1010];
cin>>t;
while(t--)
{
cin>>num;
cin>>str;
int a=strlen(str);
int b=strlen(num);
int x=0;
int flag=0;
int m=0;
for(int j=0;j<2*b;j++)
{
if(m==b)
{
flag=1;
m--;
}
if(m==0)
{
flag=0;
}
num[j]=num[m];
if(flag==0)
m++;
if(flag==1)
m--;
}
for(int i=0;i<a;i++)
{
if(x==2*b)
x=0;
if((str[i]-(num[x]-'0'))<97)
str[i]=char(str[i]-(num[x]-'0')+26);
else
str[i]=char(str[i]-(num[x]-'0'));
x++;
}
cout<<str<<endl;
//cout<<num;
}
return 0;
}
Here is my Java implementation.
import java.util.Scanner;
class sngmsg
{
public static void main(String[] s)
{
Scanner sc=new Scanner(System.in);
int t;
/*char[] str;
str = new char[1000100];
char[] num=new char[1010];*/
String str;
String num;
char[] num1=new char[1010];
char[] str1=new char[1000100];
t=sc.nextInt();
for(int l=0;l<t;l++)
{
num=sc.next();
str=sc.nextLine();
int a=str.length();
for(int c=0;c<a;c++)
str1[c]=str.charAt(c);
int b=num.length();
int x=0;
int flag=0;
int m=0;
for(int j=0;j<2*b;j++)
{
if(m==b)
{
flag=1;
m--;
}
if(m==0)
{
flag=0;
}
num1[j]=num.charAt(m);
if(flag==0)
m++;
if(flag==1)
m--;
}
for(int i=0;i<a;i++)
{
if(x==2*b)
x=0;
if(int(str1[i])-(num1[x])<97)
str1[i]=char(int(str1[i])-num1[x])+26);
else
str1[i]=char(str1[i]-(num1[x]-'0'));
x++;
}
}
}
}
In the section
if(int(str1[i])-(num1[x])<97)
str1[i]=char(int(str1[i])-num1[x])+26);
else
str1[i]=char(str1[i]-(num1[x]-'0'));
x++;
it is giving an error:
unexpected type
required:value
found: class
'.class' expected
; expected
and similar errors in these lines.
Any help?
it should be
if((int)(str1[i])-(int)(num1[x])<97)
cast operator should have () over them like (int) 1 and (char) 'a' instead of int(1) or char('a')
int and char aren't functions, but are primitives.
str1[i]=char(int(str1[i])-num1[x])+26)
You're trying to use int as a function. What do you expect from it?
Usualy, you will have a public class with your main method. It seems that you are compiling your class and you are not able to execute it. Do this to solve your problem:
1 - Create a file with your class name, example "MyClass.java"
2 - Your class declaration needs to be public:
public class MyClass {
// your code here
}
3 - Place your main method inside the created class:
public class MyClass {
public static void main( String[] args ) {
// your code here
}
}
4 - Compile it (javac MyClass.java)
5 - Execute it: java MyClass
Your code seems to have some problems too. If you want to cast some primitive or reference type, you need to put the desired type inside parenthesis. Something like:
// a char
char c = 'a';
// casting that char to an integer
int i = (int) c;
Let me know if it worked.
You are missing a package name at the start of the file. Also the class should be public.
package something.somethingelse;
import java.util.Scanner;
public class sngmsg{
Related
My teacher gave me some java code and asked me to rewrite it in python. I'm not asking for help with rewriting it, but when I entered the code into my Java compiler I got this error:
Exception in thread "main" java.lang.StackOverflowError
at
java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:449)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at java.lang.StringBuilder.<init>(StringBuilder.java:113)
at Permutations.perm1(Permutations.java:12)
at Permutations.perm1(Permutations.java:4)
Any help is greatly appreciated, here is the code:
public class Permutations {
public static void perm1(String s) {
perm1("", s);
}
private static void perm1(String prefix, String s){
int N=s.length();
if(N==0){
System.out.println(prefix);
}else{
for(int i=0; i<N; i++){
perm1(prefix+s.charAt(i)+s.substring(0, i)+s.substring(i+1,
N));
}
}
}
public static void perm2(String s){
int N=s.length();
char[] a = new char[N];
for(int i=0;i<N;i++){
a[i]=s.charAt(i);
perm2(a,N);
}
}
private static void perm2(char[] a, int n){
if(n==1){
System.out.println(a);
return;
}
for(int i=0; i<n;i++){
swap(a,i,n-1);
perm2(a,n-1);
swap(a,i,n-1);
}
}
private static void swap(char[] a, int i, int j) {
char c;
c=a[i];
a[i]=a[j];
a[j]=c;
}
public static void main(String[] args) {
int N=5;
String alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String elements = alphabet.substring(0,N);
perm1(elements);
System.out.println();
perm2(elements);
}
}
There is an error in this line:
perm1(prefix+s.charAt(i)+s.substring(0, i)+s.substring(i+1,
N));
Should look like this:
perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i + 1, N));
Compere with this code:
http://introcs.cs.princeton.edu/java/23recursion/Permutations.java.html
Stepping through the code with a debugger shows that you get a stack overflow error because of this section:
for(int i=0; i<N; i++){
perm1(prefix+s.charAt(i)+s.substring(0, i)+s.substring(i+1,N));
}
perm1 is called repeatedly, but the input doesn't change - it's always passed "abcde" with no prefix, and the result of prefix+s.charAt(i)+s.substring(0, i)+s.substring(i+1,N) is still "abcde". Since the call is recursive, and the input doesn't change with each iteration, it just repeats and takes up increasingly more space on the stack until it overflows and throws an exception.
My program is to sort numbers in ascending order and finally merge the answer which should also be in ascending order.
Compilation Errors -
**My Error****
Main.java:71: error: incompatible types
k=Integer.parseInt(res);
^
required: int[]
found: int
1 error
I had tried a lot to remove this error but could not find any logic.
**this my code**
import java.util.Scanner;
import java.util.Arrays;
public class Main
{
public static void main(String[]args)
{
int w=0,x=0;
int[] k=new int[100];
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<0)
{
System.out.println("Invalid Input");
System.exit(1);
}
int[] m=new int[n];
for(int i=0;i<n;i++)
{
m[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(m[i]>m[j])
{
int temp=m[i];
m[i]=m[j];
m[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
System.out.print(m[i]+" ");
}
w=m[n-1];
System.out.print(w);
int v=sc.nextInt();
if(v<0)
{
System.out.print("Invalid Input");
System.exit(1);
}
int[]r=new int[v];
for(int i=0;i<v;i++)
{
r[i]=sc.nextInt();
}
for(int i=0;i<v;i++)
{
for(int j=i+1;j<v;j++)
{
if(r[i]>r[j])
{
int temp=r[i];
r[i]=r[j];
r[j]=temp;
}
}
}
for(int i=0;i<v-1;i++)
{
System.out.print(r[i]+" ");
}
x=r[v-1];
System.out.print(x);
String res=" "+w+x;
k=Integer.parseInt(res);// error in this line;
Arrays.sort(k);
System.out.println(k);
}
}`
The final K is used merge the output of w and x which should also be in ascending order.
Is there any other method to resolve this.
Thank you in advance
k is an int[] (array of int) not an int.
Therefore
k = Integer.parseInt(res);
should be
k[someIndex] = Integer.parseInt(res);
You need to assign the returned value to an actual value (cell) from your array like:
for(int i=0; i<v; i++) {
....
k[i] = Integer.parseInt(res);
...
}
I agree with Eran .
incompatible types generally means left part of = and right part of = are NOT the same type. It is not a difficult error .
In your case the right part is an int while left part is int array , so just adding index to left part is fine.
I'm Writing a class with methods to send questions retrieved from a txt file stored in my computer.
My compiler keeps throwing an error at line 's=op[qno][op];' (The fourth line from the bottom) saying that an "array required but int found" .I'm not even trying to return an array so why am I seeing this? How can I correct it?
package computerproject;
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class Questions
{
String q[]=new String[30];
String op[][]=new String[55][5];
int ord[][]=new int[20][5];
Questions()throws FileNotFoundException,IOException
{
initArray();
setOrder();
}
void initArray()throws FileNotFoundException,IOException
{
BufferedReader br=new BufferedReader(new FileReader("C:\\Users\\mansoor\\Desktop\\Quiz\\Qs.txt"));
for(int i=0;i<55;i++)
{
q[i]=br.readLine();
op[i][0]=br.readLine();
System.out.print(op[i][0]);
op[i][1]=br.readLine();
op[i][2]=br.readLine();
op[i][3]=br.readLine();
op[i][4]=br.readLine();
}
}
void setOrder()
{
Random r=new Random();
for(int i=r.nextInt(55),j=0;j<40;j++,i++)
{
ord[j][0]=i%55;
}
for(int i=44;i!=0;i--)
{
int a=r.nextInt(40);
int b=r.nextInt(40);
int t=ord[a][0];
ord[a][0]=ord[b][0];
ord[b][0]=t;
for(int s[]=setRandomOrder(),j=0;j<4;j++)
{
ord[i][j]=s[j];
}
}
}
int[] setRandomOrder()
{
Random r=new Random();
int ar[]={0,1,2,3};
for(int i=0;i<16;i++)
{
int a=r.nextInt(4),b=r.nextInt(4),t;
t=ar[a];
ar[a]=ar[b];
ar[b]=t;
}
return ar;
}
String sendQuestion(int qno)
{
return q[qno];
}
String sendCorrectAnswer(int qno)
{
return op[qno][4];
}
int[] randomOrder()
{
Random r=new Random();
int ar[]=new int[4];
for(int i=0;i<16;i++)
{
int a=r.nextInt(4);
int b=r.nextInt(4);
int t=ar[a];
ar[a]=ar[b];
ar[b]=t;
}
return ar;
}
String[] sendOption(int qno,int op)
{
String s[]=new String[4];
for(int i=0;i<4;i++)
{
s[i]=op[qno][i];
}
return op[qno];
}
}
You arent passing in an array to your sendOption method. You're passing in an integer ( op ) , then treating it as your previously declared 2D array when placing the s[i] values in (op[qno][i]). The error could be here. I would change the name of your second input parameter. Hope this helps!
Look at your method signature sendOption(int qno,int op). The value op is of type int. It is not an array that means you can't do stuff like op[qno][i]
I am having a String , i am creating a BIT of String based on the frequency of the element present
String:
abcdbcaab
Code:
class Test{
static int[][] dp;
public static void update(int i , int val ,int[] dpp){
while(i<=100000){
dpp[i]+=val;
i+= (i&-i);
}
}
public static int value(int i ,int[] dp){
int ans =0;
while(i>0){
ans+=dp[i];
i-= (i&i);
}
return ans;
}
public static void main(String args[] ) throws IOException {
Scanner in = new Scanner(new InputStreamReader(System.in));
dp = new int[27][1000001];
String s = in.next();
for(int i=0;i<s.length();i++){
update(i+1,1,dp[s.charAt(i)-'a']);
}
System.out.println(dp[0][7]); // Should show 2 as the frequency of 'a' at 7 position is 2
}
}
Where i am doing wrong . i could not get it but dp[0][8] is showing me 3Please Help i could not figure it out where i have commit mistake
Negativity should be avoided in life !!! but i think a little more negativity can improve you'r code:
value function you are doing wrong decrement of value of i
public static int value(int i ,int[] dp){
int ans =0;
while(i>0){
ans+=dp[i];
i-= (i&i); // Should be (i&-i);
}
return ans;
}
So the first part creates a vector and adds a digit to the 10 slots. Then after this nothing happens, i have no errors in my code but it just stops.. why?
package ovn7;
import java.util.Scanner;
public class ovn7a {
int []vektor;
Scanner scan = new Scanner(System.in);
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
}
public int find(int tal) {
System.out.println("tal");
tal = scan.nextInt();
int i = 0;
while(i<10 && vektor[i] != tal) {
i++;
}
return (i <10) ? i : -1;
}
}
This is your main method:
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
}
That's all your program does - when it hits the closing right brace of the main method, execution ends. If you want it to execute public int find(int tal) as well, you need to include a method call to your main method:
int index = find(5); //for example
Remember, the main method is the only one that is called automatically when executing the program! You'll have to call find yourself inside main.
EDIT: per request, an example of main with the method call included:
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
int index = find(5); // <-- this find(5) here is a method call for find!
System.out.println("The method returned a value of " + index + ".");
}
You can replace that "5" with any integer, as the method find accepts an integer as an argument. (as a side note, it doesn't matter which integer you pass to find - it overwrites the argument with a new value anyway)