2 Replies - 102 Views - Last Post: 04 February 2012 - 09:37 PM Rate Topic: -----

Topic Sponsor:

#1 tarakandi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-February 12

Can you explain me these lind of codes?

Posted 04 February 2012 - 05:43 PM

This is a code for Palindromes
Hello guys!!!
I'm new to java programming!
Just started last semester.
Help me in the places I have Ariel black

    String P, // Ignore these set of codes.. I know what's happening here.
    str;
    int i;

    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter a Word: ");
    P = keyboard.nextLine();

    // I'm looking at the sample code and didn't get these line of code.

    int n = P.length(); // Does the variable n stands for the length of
                        // user's input?
    str = ""; // why is str set to blank?

    for (i = n - 1; i >= 0; i--) // I don't get this loop
        str = str + P.charAt(i); // and this

    if (str.equals(P)) // and this line of code.
        System.out.println(P + " is palindrome");
    else
        System.out.println(P + " is not a palindrome");



Thank you!!!!!

This post has been edited by blackcompe: 04 February 2012 - 06:09 PM
Reason for edit:: Please use [code] tags when posting to the forum.


Is This A Good Question/Topic? 0
  • +

Replies To: Can you explain me these lind of codes?

#2 M4trixSh4d0w  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 07-May 10

Re: Can you explain me these lind of codes?

Posted 04 February 2012 - 05:56 PM

Please put your code in Code tags it makes it easier to read.


int n=P.length(); //Does the variable n stands for the length of user's input?  P.length() is the length of the string P.
//for example, if P is equal to "Doggie" 
//then P.length() = 5
//the String "Doggie" is 6 characters, except the computer starts counting from 0, so its actually 5.
str = ""; // why is str set to blank? To initialize the string, otherwise its NULL which is nothing. so that you don't have any problems later 

for (i = n-1; i>=0; i--) //I don't get this loop 
// integer i = n - 1;
// keep doing whats in the brackets as long as i >= 0
//i-- every time you finish the code in the brackets, minus i by 1

str=str+P.charAt(i); // and this
//string str =  str + P.characterAt(Index)
//like if P is "Doggie" then, P.charAt(2) = "g";
//0. D  computers start counting from 0
//1. o 
//2. g 


if(str.equals(P)) //and this line of code.
//if String str is equal to String P
//like "Doggie" = "Doggie"
System.out.println(P+ " is palindrome");
else
System.out.println(P+ " is not a palindrome");
}



(answers in the code)
Was This Post Helpful? 2
  • +
  • -

#3 tarakandi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-February 12

Re: Can you explain me these lind of codes?

Posted 04 February 2012 - 09:37 PM

Thanks a lot :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1