Results 1 to 10 of 10
  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Checking chars in strings

    What I want to do, is generate an int, change it to a string, and then compare the first and last chars, the 2nd last and 2nd chars (to see if they're the same) to check whether it's symmetrical. I need to find numbers like 1001, 44044, 181181, etc.

    I've been searching, but I still don't know what to use to check the string like this?

    Cheers!

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Checking chars in strings

    If you want to work with strings, you should look at String (Java Platform SE 6).

    Specifically, you want to look at the following String functions: valueOf, getChars or substring.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Checking chars in strings

    Or you can simply create a reversed version of the string. And then compare it to the original string to see if they have the same contents.
    Last edited by wsaryada; 04-24-2012 at 08:40 AM.

  4. #4
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Checking chars in strings

    Java Code:
    public class Main{
    	
    	public static void main(String[] args){
    		
    	String num=ranGen(5);
    
    	System.out.println(num+" is an object of "+num.getClass());
    
    	System.out.println(isSymmetrical(num));
    		
    	}
    
    	public static String ranGen(int digits){
    		
    		int num=(int)(Math.pow(10,digits)*Math.random());
    		
    		return String.valueOf(num);
    	}
    
    	public static boolean isSymmetrical(String str){
    		
    		int length=str.length();
    		int index=0;
    		boolean result=true; 
    
    		while(index<(int)(length/2) && result){
    		
    			if(str.charAt(index)!=str.charAt(length-1-index)){
    				
    				result=false;
    			}
    			
    			index++;
    		}
    		return result;
    	}
    }

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Checking chars in strings

    @Shall, cheers, that's what I've been looking at but I don't understand some of the definitions. CharAt was what I wanted.

    @wsaryada: Yeah you're right. Originally I thought it wouldn't work because it would lose zeros (so 19910 would become 1991), but it doesn't when it's a string. I've been trying to look at other peoples code to figure out how to do that, and not really having much luck. But I will keep trying, maybe I will make two different codes for this problem.

    @kosmos, yep! That's basically what I want to do. Get the length of the string, divide it by 2 set int x, set up two charAt's to loop up to x from 1, and down to x from length, comparing char against each other. When != ;break and go to next number.

    Need to find the highest palindromic number that's a product of 2 three digit numbers (Euler's Project: Problem 4: http://projecteuler.net/), which is why I'm going from 999 downwards.

    Code I am working on (Full of errors):

    Java Code:
    public class Problem4 {
    	public static void main (String args[]){
    		int x, y, z, a;
    		a = 0;
    				
    		for (x=999;x>=900;x--){
    			for (y=999; y>=900; y--){
    				z=x*y;
    				a += z;
    				String Zstring = Integer.toString(a);
    				int len=Zstring.length();
    				char checkfirst = Zstring.charAt(1);
    				char checklast = Zstring.charAt(len);
    				if (checkfirst == checklast){
    					System.out.println(z);
    				}
    
    			}
    			
    		}
    	}
    }
    When I run it I get:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
    at java.lang.String.charAt(Unknown Source)
    at Problem4.main(Problem4.java:13)
    And if I click on "at Problem4.main(Problem4.java:13)" It highlights line 13 "char checklast = Zstring.charAt(len);".

    From testing, if I remove line 13 (and and varible in 13 from the code) it works. So... I don't know what that means, I can't have two charAts?

    More testing: It doesn't like char checklast = Zstring.charAt(len);

    If I put a number there instead of "len" then it works. And I can print len, and it's the right number. I tried to z+=len, and put z there instead of len, that didn't work either (don't know why it would, lol).

    What is going on? len is just an int isn't it?
    Last edited by Zigster; 04-24-2012 at 11:13 AM.

  6. #6
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Checking chars in strings

    This is my new code:
    Java Code:
    public class Problem4v1 {
    	public static void main (String[] args){
    		
    		int a, x, y, z, b, c;
    		x = 999;
    		y = 999;
    		
    		for (x=999; x>99; x--)
    		{
    			for (y=999; y>=99; y--)
    			{
    			a=x*y;
    			z=0;
    			b=0;
    			c=1;
    			String Zstring = Integer.toString(a);
    			int len=Zstring.length();
    			int check1 = Zstring.charAt(b++);
    			int check2 = Zstring.charAt(len-c++);
    			{
    				
    			while (b <= len/2){
    				
    				if (check1 != check2){
    					break;
    				}
    				else {System.out.println("This number is a Palindrome:\t" + a);			
    				}
    				}
    				}
    			}
    		}
    	}
    
    }
    It prints out:

    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    This number is a Palindrome: 990009
    Getting closer... if I -1 from len then it works, so that might be why it is one digit out of sync. But it prints 990009 like probably thousands of times, have I created an infinite loop?

    Update:

    Code:

    Java Code:
    public class Problem4v1 {
    	public static void main (String[] args){
    		
    		int a, x, y, c, d;
    		x = 999;
    		y = 999;
    		d = 0;
    		
    		for (x=999; x>99; x--)
    		{
    			for (y=999; y>=99; y--)
    			{
    			a=0;
    			a=x*y;
    			c=1;
    			String Zstring = Integer.toString(a);
    			int len=Zstring.length();
    			
    				while (c <= len/2)
    				{		
    				int check1 = Zstring.charAt(c);
    				int check2 = Zstring.charAt(len-c);
    				
    					if (check1 != check2)
    					{
    					break;
    					}
    					
    					
    					if (d<a)
    					{
    					d=0;
    					d+=a;
    					}
    					
    				c++;
    				}
    			}
    		}
    		
    	System.out.println("This is the highest Palindrome:\t" + d);
    }
    }
    Prints Out:

    This is the highest Palindrome: 994009
    So close yet so far
    Last edited by Zigster; 04-24-2012 at 12:56 PM.

  7. #7
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Checking chars in strings

    Java Code:
    public class Main{
    	
    	public static void main(String[] args){
    		
    		 String numStr;	
    		 int max=0, num,factor1=999,factor2=999;		
    
    		for(int i=999;i>=100;i--)
    			for(int j=999;j>=100;j--){
    			
    				num=i*j;
    				numStr=Integer.toString(num);
    
    				if(num>max && isSymmetrical(numStr)){
    					max=num;
    					factor1=i;
    					factor2=j;
    				}
    			}
    		System.out.println(factor1+"*"+factor2+"="+max);	
    	}
    }

  8. #8
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Checking chars in strings

    Hey kosmos, I tried your code but I got an error:


    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method isSymmetrical(String) is undefined for the type Main

    at Main.main(Main.java:14)
    Cheers!

  9. #9
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Checking chars in strings

    Copy paste the function boolean isSymmetrical(String str) from my post #4 inside Main class.

  10. #10
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Checking chars in strings

    Thanks kosmo, it works! Gonna try and use multiple methods in the future.
    Last edited by Zigster; 04-24-2012 at 08:04 PM. Reason: spelling

Similar Threads

  1. Replies: 2
    Last Post: 07-21-2011, 03:46 AM
  2. Checking for two strings in array at the same time
    By veronique in forum New To Java
    Replies: 10
    Last Post: 04-27-2011, 05:36 AM
  3. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  4. List of chars
    By SwEeTAcTioN in forum New To Java
    Replies: 8
    Last Post: 01-05-2010, 12:05 PM
  5. chars
    By whosadork in forum New To Java
    Replies: 6
    Last Post: 10-03-2008, 09:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •