38 Replies - 4920 Views - Last Post: 28 November 2011 - 09:33 PM
#16
Re: Array Error
Posted 27 November 2011 - 07:16 PM
Declare a new StringBuilder (sb), count down length-1 to zero, appending charAt(currentCount) to sb. Get to zero, return sb.toString and have a beer, because you're done.
#17
Re: Array Error
Posted 27 November 2011 - 07:19 PM
public static String reverse(String str) {
StringBuilder sb = new StrinBuilder(str);
return sb.reverse().toString();
}
*Edited: the method returns a String as jon.kiparsky noted
This post has been edited by pbl: 27 November 2011 - 07:46 PM
#18
Re: Array Error
Posted 27 November 2011 - 07:21 PM
#19
Re: Array Error
Posted 27 November 2011 - 07:22 PM
This post has been edited by jon.kiparsky: 27 November 2011 - 07:22 PM
#20
Re: Array Error
Posted 27 November 2011 - 07:49 PM
so what can I do to correct my code?
#21
Re: Array Error
Posted 27 November 2011 - 07:50 PM
jon.kiparsky, on 27 November 2011 - 10:22 PM, said:
16 posts of the OP trials before understanding what an array is ....
I think I've tried the pedagogical approach enough
Won't expand this thread to 200 replies if he does not get it. I have tried on 10 posts
#22
Re: Array Error
Posted 27 November 2011 - 07:52 PM
#23
Re: Array Error
Posted 27 November 2011 - 07:53 PM
jon.kiparsky, on 27 November 2011 - 10:22 PM, said:
Agree with you... our shortcuts work but you have to write your own
so my reply with
char[] digit = str.toCharArray(); int k = digit.length; char[] reverse = new char[k];
should satisfy your teacher
#24
Re: Array Error
Posted 27 November 2011 - 07:58 PM
#25
Re: Array Error
Posted 27 November 2011 - 08:01 PM
Lets face the reallity of a homework to produce here tonigh !!!
#26
Re: Array Error
Posted 28 November 2011 - 06:38 PM
example. if I put in : 012
the out put is : 21
or if I put in : 120
the out put is : 21
public String Revers(String str) {
char[] charArr = str.toCharArray();// So I can know how many numbers are
// there.
int[] fstVar = new int[charArr.length];// First Variable.
int[] sndVar = new int[charArr.length];// Second Variable.
int strint = Integer.parseInt(str);// Input as integer.
do {
int x = 1;
fstVar[x] = strint % 10;
sndVar[x] = sndVar[x] * 10 + fstVar[x];
strint = strint / 10;
} while (strint > 0);
do {
int j = 1;
System.out.println("The reverse number : " + sndVar[j]);
} while (strint > 0);
return str;//Just to make the function happy
}
#27
Re: Array Error
Posted 28 November 2011 - 06:51 PM
int strint = Integer.parseInt(str);
Also I dont see the use of this do loop here, because it will always execute once:
do {
int j = 1;
System.out.println("The reverse number : " + sndVar[j]);
} while (strint > 0);
Also you declare and set your x and j to 1, so every iteration you x will be 1 and so dealing with index 1 everytime, so what is the use of array here?
#28
Re: Array Error
Posted 28 November 2011 - 06:51 PM
public String reverse(String str) {
int len = str.length();
char[] rev = new char[len];
for(int i = 0; i < str.length(); ++i)
rev[--len] = str.charAt(i);
return new String(rev);
}
#29
Re: Array Error
Posted 28 November 2011 - 06:52 PM
public static void main(String... args) {
Scanner input = new Scanner(System.in);
String numstr = input.next();
System.out.println(numstr);
int numint = Integer.parseInt(numstr);
System.out.println(numint);
// 0's will be removed
}
// output
012345 <-- input
012345
12345
This post has been edited by Mylo: 28 November 2011 - 06:53 PM
#30
Re: Array Error
Posted 28 November 2011 - 07:00 PM
pbl, on 28 November 2011 - 06:51 PM, said:
public String reverse(String str) {
int len = str.length();
char[] rev = new char[len];
for(int i = 0; i < str.length(); ++i)
rev[--len] = str.charAt(i);
return new String(rev);
}
Thank you SO much. I'm so sorry if I was too naive. sorry again.
|
|

New Topic/Question
Reply




MultiQuote






|