Can any one fix errors(exceptions)...... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can any one fix errors(exceptions)......

import java.util.Scanner; class Waste{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); char AL,AR,BU,BD; int z=0,y=0; int p=0; int k=sc.nextInt(); char a[]=new char[k]; char b[]=new char[k]; char t; String s=sc.nextLine(); b[0]=a[0]; for(int i=0;i<k;i++) { a[i]=s.charAt(i); if((a[0]=='L')|(a[0]=='R')) if((a[i]!='L')|(a[i]!='R')) { p++; b[p]=a[i]; t=a[0]; a[0]=a[i]; a[i]=t; } if((a[0]=='U')|(a[0]=='D')) if((a[i]!='U')|(a[i]!='D')) { p++; b[p]=a[i]; t=a[0]; a[0]=a[i]; a[i]=t; } } for(int i=0;i<k;i++) { if(b[i]=='\0') break; switch(b[i]) { case 'R': z+=1; break; case 'L': z-=1; break; case 'U': y+=1; break; case 'D': y-=1; break; } } System.out.println("("+z+","+y+")")

26th Mar 2020, 4:04 AM
V Chandu
V Chandu - avatar
5 Answers
+ 1
first, there is not end of code and semicolon System.out.println("("+z+","+y+")"); //semicolon ; } //added } //added
26th Mar 2020, 4:21 AM
zemiak
+ 1
zemiak its not a problem rest of code i.e., ; }} are not there because there were limit of 2400 words only I meant there were problem in exception so concentrate on exceptions not in run time errors
26th Mar 2020, 5:31 AM
V Chandu
V Chandu - avatar
+ 1
for(int i=0;i<k;i++) Here instead of k you should use, String length.. s.length(). Otherwise you get Exception if your string length is less than k.. Or invalid result if greater than k.. May be you are know this.. | is bitwise Or operator || is logical Or operator. Edit: You can save code in playground and share link that help to identify errors easily... See this link to know how to share.. https://www.sololearn.com/post/75089/?ref=app
26th Mar 2020, 7:27 AM
Jayakrishna 🇮🇳
0
Input:LLLRUUD output:LU input:LULUDR Output:LULUR The firstly point at origin and again It can't make multimoves on same axis consequently...
26th Mar 2020, 4:07 AM
V Chandu
V Chandu - avatar
0
V Chandu //use correct scan int, for int\n // int n=sc.nextInt(); int n=sc.nextInt(); sc.nextLine(); ... //after last scan, a[0] is empty // b[0]=a[0]; b[0]=s.charAt(0); ... if( (a[0]=='L')|(a[0]=='R')) //works but standard is || { // added, necessary because two if and one else // if((a[i]!='L') | (a[i]!='R')) if ((a[i]!='L') && (a[i]!='R')) // AND instead {... } } else // added if( (a[0]=='U')|(a[0]=='D')) // if((a[i]!='U') | (a[i]!='D')) if ((a[i]!='U') && (a[i]!='D')) // AND instead {...}
26th Mar 2020, 10:43 PM
zemiak