Results 1 to 13 of 13
Thread: Strings
- 04-18-2011, 12:19 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
- 04-18-2011, 12:29 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
Because the object that "a" originally pointed to is unchanged by the above operations. A new object has been created and "a" now points to that, but "a" is not the String, is a reference to a String (and that reference can change to point to a new String), but the String itself does not.
Edit: See this then this.Last edited by masijade; 04-18-2011 at 12:31 PM.
- 04-18-2011, 12:41 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
suppose I have an array 12,34,35,42,9,76,31,54,99,150
and I have to find the element which is a prime number and print the element and index position of the array as well using break statement.
Can you please provide me the program as I am stuck in the beginning itself i.e. finding the prime number from the array.
- 04-18-2011, 01:00 PM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 286
- Rep Power
- 11
How to find a prime number
Swastik
- 04-18-2011, 01:05 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
I am done answering your quiz questions.
- 04-18-2011, 01:45 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Hi Dswastik,
I went through the link but it didnt help me much. I need to search the prime number from an array of elements 12,34,35,42,9,76,31,54,99,150. So was unable to do it. I tried little but its giving Array Index Outof Bounds...
My coding is as follows..
class Break
{
public static void main(String ar[])
{
int a[]={12,34,35,42,9,76,31,54,99,150};
for(int i=0;i<=a.length;i++)
{
int n=a[0];
for(int j=2;j<a[i];j++)
{
n=a[i]%j;
if(n==0)
System.out.println("Not Prime");
break;
}
if(n==a[i])
System.out.println(a[i]);
}
}
}
The first for loop is regarding the element index, I am ignoring that first I need to find out the prime number.
- Java Code:
for([COLOR="blue"]int i[/COLOR]=0;i<[COLOR="Red"]=[/COLOR]a.length;i++)
i cannot equal-to a.length because, in computers, everything starts with a zero.
That means, if you have an array with 5 objects, the indexes would be 0,1,2,3,4 but the index i cannot equal the array length = 5. Otherwise you would get an IndexOutOfBounds exception.
- 04-18-2011, 02:04 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
- 04-18-2011, 02:05 PM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 286
- Rep Power
- 11
To add more, its better to write your codes in small block of functions, its always easier to debug
Java Code:class Break { private static boolean isPrimeNumber(int num){ int i; for (i=2; i < num ;i++ ){ int n = num%i; if (n==0){ break; } } if(i == num){ return true; } else{ return false; } } public static void main(String ar[]) { int a[]={12,34,35,42,9,76,31,54,99,150}; for(int i=0;i<a.length;i++) { int num=a[i]; if(!isPrimeNumber(num)){ System.out.println(num+" is not a prime number"); //break; } else{ System.out.println(num+" is a prime number"); } } } }
Swastik
- 04-18-2011, 02:32 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Build a wall around Donald Trump; I'll pay for it.
- 04-18-2011, 02:34 PM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
WooHoo! Chaos made to order! ;-)
- 04-18-2011, 02:37 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
Thanks a lot Dswastik... :)
- 04-18-2011, 02:43 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Yeah, yeah, laugh about it; but even in the book "Numerical Recipies in C" I saw them do things like this:
Java Code:float farray[100]; float* parray= &(farray[-1]);
The boneheads didn't realize that the expression farray[-1] causes undefined behaviour ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
Need help with Strings
By </3java in forum New To JavaReplies: 24Last Post: 02-17-2011, 06:00 PM -
Strings
By zoala001 in forum Java AppletsReplies: 9Last Post: 01-02-2011, 11:26 AM -
Binary Strings
By Zack in forum New To JavaReplies: 0Last Post: 06-25-2010, 08:15 AM -
Strings and Immutable
By al_Marshy_1981 in forum New To JavaReplies: 19Last Post: 06-18-2010, 08:22 AM -
It is possible in Strings..?
By mlibot in forum New To JavaReplies: 1Last Post: 03-12-2010, 06:30 AM
Bookmarks