Array Error

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

38 Replies - 5160 Views - Last Post: 28 November 2011 - 09:33 PM Rate Topic: -----

#16 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,736
  • Joined: 19-March 11

Re: Array Error

Posted 27 November 2011 - 07:16 PM

So, a string. Okay, forget the arithmetic in my last post, just slap the chars onto the front end of a StringBuffer as you pull them off the back end of the original String - or vice versa - and you're good.

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.
Was This Post Helpful? 0
  • +
  • -

#17 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Error

Posted 27 November 2011 - 07:19 PM

a lot easier jon, the StringBuilder and StringBuffer class have a reverse() method

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

Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Array Error

Posted 27 November 2011 - 07:21 PM

@pbl: Don't forget the String return type in your method as well! :)
Was This Post Helpful? 1
  • +
  • -

#19 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,736
  • Joined: 19-March 11

Re: Array Error

Posted 27 November 2011 - 07:22 PM

@pbl - Obviously, but as always I'm assuming that some vague twitching of effort on the student's part is expected.

This post has been edited by jon.kiparsky: 27 November 2011 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#20 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 154
  • Joined: 08-September 11

Re: Array Error

Posted 27 November 2011 - 07:49 PM

@pbl - thank you so much for this way, but I'm sure if I used it my mentor will say I NEED YOU TO USE THE WAY I TOLD YOU! NO SHORT CUTS!

so what can I do to correct my code?
Was This Post Helpful? 0
  • +
  • -

#21 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Error

Posted 27 November 2011 - 07:50 PM

View Postjon.kiparsky, on 27 November 2011 - 10:22 PM, said:

@pbl - Obviously, but as always I'm assuming that some vague twitching of effort on the student's part is expected.

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 :)
Was This Post Helpful? 0
  • +
  • -

#22 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Array Error

Posted 27 November 2011 - 07:52 PM

So let's say you're given an array. In order to reverse the array you want to swap the first and last elements (0 and length-1), then you want to swap elements (1 and length-2), then swap elements (2 and length-3). See the pattern. The stopping point is in the middle. Think about how you would write a loop to do this.
Was This Post Helpful? 1
  • +
  • -

#23 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Error

Posted 27 November 2011 - 07:53 PM

View Postjon.kiparsky, on 27 November 2011 - 10:22 PM, said:

@pbl - Obviously, but as always I'm assuming that some vague twitching of effort on the student's part is expected.

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
Was This Post Helpful? 0
  • +
  • -

#24 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,736
  • Joined: 19-March 11

Re: Array Error

Posted 27 November 2011 - 07:58 PM

mac's is one approach. Another would be, as I've suggested, to create a new array and place the item from original[length-1] in new[0], then original[length-2] in new[1], then original[length-3] in new[2], and so forth. Think of this as original[(length-1)-i] and new[0+i] and it might be easier to see how the loop works.
Was This Post Helpful? 0
  • +
  • -

#25 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Error

Posted 27 November 2011 - 08:01 PM

Friends, the OP does not give a s*** about your philosopical discussions :)
Lets face the reallity of a homework to produce here tonigh !!!
Was This Post Helpful? 0
  • +
  • -

#26 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 154
  • Joined: 08-September 11

Re: Array Error

Posted 28 November 2011 - 06:38 PM

Thank you so much guys. I changed it to a do loop and it worked almost fine, every thing works, but when I type 0 in the beginning or the end o the input it doesn't display it or skips it. why?
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

	}


Was This Post Helpful? 0
  • +
  • -

#27 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Array Error

Posted 28 November 2011 - 06:51 PM

When parsing to integer you will miss the starting 0s.
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?
Was This Post Helpful? 1
  • +
  • -

#28 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Error

Posted 28 November 2011 - 06:51 PM

You are reversing a String not number, why do you need the modulo operator

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);
}


Was This Post Helpful? 1
  • +
  • -

#29 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Array Error

Posted 28 November 2011 - 06:52 PM

I think your problem is this:

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

Was This Post Helpful? 0
  • +
  • -

#30 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 154
  • Joined: 08-September 11

Re: Array Error

Posted 28 November 2011 - 07:00 PM

View Postpbl, on 28 November 2011 - 06:51 PM, said:

You are reversing a String not number, why do you need the modulo operator

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.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3