Array Error

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

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

#1 amture106  Icon User is offline

  • D.I.C Head

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

Array Error

Posted 27 November 2011 - 05:42 PM

Hey guys,

I'm writing a program that takes a number and reverse it. So far I think I was doing fairly not bad, but then I got this error massage.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Reversing.Revers(Reversing.java:9)
at Main.main(Main.java:11)

Here is my main class
import java.util.Scanner;


public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter a number: ");
		String numstr = scan.nextLine();		
		Reversing rv = new Reversing();
		rv.Revers(numstr);
		
	
}
}



and here is my reversing class

public class Reversing {
	public String Revers(String str){
		char[] charArr =  str.toCharArray();//So I can know when the for loop should stop.
		int [] fstVar = { };//First Variable.
		int sndVar;//Second Variable.
		int strint = Integer.parseInt(str);//Input as integer.
		for (int x = 0; x < charArr.length - 1 ; x++){
			fstVar[x] = strint % 10;
			for (int i = 0; i < charArr.length - 1 - i; i++){
				sndVar = (fstVar[i] *10 ) + fstVar[i];
			}
		}
		for (int j = 0; j < charArr.length - 1; j++){
		str = Integer.toString(fstVar[j]);
		System.out.println(str);
		}
		return str;
	}
}



may any one please tell me what I've done wrong this time.

Is This A Good Question/Topic? 0
  • +

Replies To: Array Error

#2 pbl  Icon User is offline

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

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

Re: Array Error

Posted 27 November 2011 - 05:50 PM

int [] fstVar = { };//First Variable.

that creates an array of 0 length int[0];

that will try to access

fstVar[x] = strint % 10;

fstVar[0], fstVar[1], ... even the first one fstVar[0] is out of bound
Was This Post Helpful? 0
  • +
  • -

#3 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 - 05:53 PM

I corrected that, but still same error
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

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

Re: Array Error

Posted 27 November 2011 - 06:00 PM

View Postamture106, on 27 November 2011 - 08:53 PM, said:

I corrected that, but still same error

Corrected it how ?
Was This Post Helpful? 0
  • +
  • -

#5 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 - 06:03 PM

public class Reversing {
	public String Revers(String str){
		char[] charArr =  str.toCharArray();//So I can know when the for loop should stop.
		int [] fstVar = { };//First Variable.
		int [] sndVar = {};//Second Variable.
		int strint = Integer.parseInt(str);//Input as integer.
		for (int x = 1; x < charArr.length; x++){
			fstVar[x] = strint % 10;
			for (int i = 1; i < charArr.length - 1; i++){
				sndVar[i] = (fstVar[i] *10 ) + fstVar[i];
			}
		}
		for (int j = 1; j < charArr.length; j++){
		str = Integer.toString(fstVar[j]);
		System.out.println(str);
		}
		return str;
	}
}


This post has been edited by amture106: 27 November 2011 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7731
  • View blog
  • Posts: 13,058
  • Joined: 19-March 11

Re: Array Error

Posted 27 November 2011 - 06:07 PM

fstVar and sndVar are still initialized to zero-length arrays.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: Array Error

Posted 27 November 2011 - 06:08 PM

        int [] fstVar = { };//First Variable.
        int [] sndVar = {};//Second Variable.


Don't see what you corrected :)
These 2 arrays have a length of 0. They are completly useless
you cannot access any element in them. as they don't have any, which you do
            fstVar[x] = strint % 10;    // here
            for (int i = 1; i < charArr.length - 1; i++){
                sndVar[i] = (fstVar[i] *10 ) + fstVar[i];  // and here 3 times


Was This Post Helpful? 0
  • +
  • -

#8 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 - 06:13 PM

but if I made it like
int [] fstVar; 
int [] sndVar;


it gives me an error too and say variable must be intinilazed and eclips intinilize it as null and null can't be accessed.

so what to do?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

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

Re: Array Error

Posted 27 November 2011 - 06:28 PM

View Postamture106, on 27 November 2011 - 09:13 PM, said:

but if I made it like
int [] fstVar; 
int [] sndVar;


it gives me an error too and say variable must be intinilazed and eclips intinilize it as null and null can't be accessed.

so what to do?

Sure they are not initialized... what are you planning to do with these two arrays ?
Was This Post Helpful? 0
  • +
  • -

#10 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 - 06:40 PM

well I want to do with fstVar is save remain from input number / 10 as long as there are numbers in the charArr
then sndVar to save the (fstVar * 10) + fstVar in a loop count until charArr has nothing in it.

and hopefully by this way I can reverse numbers.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

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

Re: Array Error

Posted 27 November 2011 - 06:46 PM

So should be
int[] fstVar = new int[charArr.length];
int[] sndVar = new int[charArr.length];

But this will do nothing
        int strint = Integer.parseInt(str);//Input as integer.
        fstVar[x] = strint % 10;   // that integer modulo 10
            for (int i = 1; i < charArr.length - 1; i++){
                // modulo 10 * 10 + value
                sndVar[i] = (fstVar[i] *10 ) + fstVar[i];


So if string is 56
fstBar[] = strint % 10 = 5
sndVar[] = 5 * 10 + 5 == 55

What the hell are you trying to do ?
Was This Post Helpful? 1
  • +
  • -

#12 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 - 06:54 PM

reversing number
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

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

Re: Array Error

Posted 27 November 2011 - 07:02 PM

how ?
What are you doing with the % operator on a char ?

public static String reverse(String str) {
   char[] digit = str.toCharArray();
   int k = digit.length;
   char[] reverse = new char[k];
   for(int i = 0; i < digit.length; ++i)
      reverse[--k] = digit[i];
   return new String(reverse);
}


Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7731
  • View blog
  • Posts: 13,058
  • Joined: 19-March 11

Re: Array Error

Posted 27 November 2011 - 07:05 PM

View Postamture106, on 27 November 2011 - 08:40 PM, said:

well I want to do with fstVar is save remain from input number / 10 as long as there are numbers in the charArr
then sndVar to save the (fstVar * 10) + fstVar in a loop count until charArr has nothing in it.

and hopefully by this way I can reverse numbers.


In that case you should figure out how many places you need in the array and initialize it to hold that many places.

But why do you want to put the remainders into an array? Couldn't you just construct the reversed integer?

Start with 1234567
int reversed = 0;
lop off the 7, add it to reversed.
multiply reversed by 10, lop off the 6, and add it to reversed.
Repeat last step as long as there's anything left of the first number.

public String Revers(String str){


Wait... I'm confused, are you trying to reverse a String or a number?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

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

Re: Array Error

Posted 27 November 2011 - 07:11 PM

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

Wait... I'm confused, are you trying to reverse a String or a number?

A number contained in a String
Was This Post Helpful? 0
  • +
  • -

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