CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Java Challenge

  1. #1
    Join Date
    Dec 2011
    Posts
    5

    Java Challenge

    Am to write a program that takes a string of digits, S and then caluclates the next term S1 of the sequence.
    For Example, if S is "10444221" then S is described from left to right as
    "one 1, one 0, three 4's, two 2's, one 1"
    This string is converted into digits from left to right resulting in an S1 value of "1110342211".
    Anybody have idea on how the program will be in java..Thank you

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Challenge

    Anybody have idea on how the program will be in java.
    Yes
    Thank you
    My pleasure.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Dec 2011
    Posts
    5

    Re: Java Challenge

    [QUOTE]Yes
    do u hava idea of the code.......

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Challenge

    Yes, but no one here is going to do your homework for you. Show us what you have done so far, explain where you are stuck, ask a question and we will try to answer it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Dec 2011
    Posts
    5

    Re: Java Challenge

    String str = "1122313";
    char arr[] = str.toCharArray(); // convert the String object to array of char
    System.out.println("INput "+str);

    // iterate over the array using the for-each loop.
    int i=0;
    int [] b= new int [str.length()];
    for (i=0;i<str.length();i++){

    System.out.println("array :"+arr[i]);
    //if ( arr[i]== arr[i++] && arr[i++]!=0)
    if(arr[i]=='0')
    {
    b[i] = +1;
    // a1++;
    }
    if(arr[i]=='1' &&arr[i++]==arr[i])
    {
    b[i]=+2;
    }
    else
    b[i]=+1;
    if(arr[i]=='2' &&arr[i++]==arr[i] )
    {
    b[i]=+2;
    }
    else b[i]=+1;
    This is where am Stuck..My code will only compare the first two character and add an increment to the b[] array counter if the two characters are equal are 2 and if false add 1...Anybody have idea on how to compare all the characters and also display the input with the counter together...Thank you

  6. #6
    Join Date
    Dec 2011
    Posts
    1

    Re: Java Challenge

    Aaha..This is difficult task.I need to do some homework to crack down this code.Kitchen cabinets

  7. #7
    Join Date
    Nov 2011
    Posts
    189

    Re: Java Challenge

    I understand nothing...

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Challenge

    Your code can't possibly work. If you look at the question you will see the intermediate result and final string are not the same length as the starting string so why have you declared the array b to be the same length as str?

    Do you have to store and output the intermediate result or do you just need to output the final string?

    Before you go any further you need to get a pen and paper and run through the example yourself writing down how you solve each step. When you have a series of written instructions that work for any input you have the strategy you need to convert to code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Dec 2011
    Posts
    4

    Resolved Re: Java Challenge

    Hey guys here is the solution:


    package com.test.jguru;

    import java.util.Hashtable;
    import java.util.Map;

    public class ChallengeNumber {

    /**
    * @param args
    */
    public static void main(String[] args) {
    Map<Integer,String> mapNum = new Hashtable<Integer, String>();
    mapNum.put(1, "one");
    mapNum.put(2, "two");
    mapNum.put(3, "three");
    mapNum.put(4, "four");
    mapNum.put(5, "five");
    mapNum.put(6, "six");
    mapNum.put(7, "seven");
    mapNum.put(8, "eight");
    mapNum.put(9, "nine");

    String strNum = "10444221";
    System.out.println("String Length "+strNum.length());
    int count = 0;
    for(int i = 0;i<strNum.length();i++){
    if(!(i+1 == strNum.length())){
    if(strNum.charAt(i) == strNum.charAt(i+1)){
    count++;
    }else{
    System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
    count = 0;
    }
    } else{
    System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
    }
    }

    }

    }

  10. #10
    Join Date
    Dec 2011
    Posts
    4

    Re: Java Challenge

    Quote Originally Posted by [email protected] View Post
    Hey guys here is the solution:


    package com.test.jguru;

    import java.util.Hashtable;
    import java.util.Map;

    public class ChallengeNumber {

    /**
    * @param args
    */
    public static void main(String[] args) {
    Map<Integer,String> mapNum = new Hashtable<Integer, String>();
    mapNum.put(1, "one");
    mapNum.put(2, "two");
    mapNum.put(3, "three");
    mapNum.put(4, "four");
    mapNum.put(5, "five");
    mapNum.put(6, "six");
    mapNum.put(7, "seven");
    mapNum.put(8, "eight");
    mapNum.put(9, "nine");

    String strNum = "10444221";
    System.out.println("String Length "+strNum.length());
    int count = 0;
    for(int i = 0;i<strNum.length();i++){
    if(!(i+1 == strNum.length())){
    if(strNum.charAt(i) == strNum.charAt(i+1)){
    count++;
    }else{
    System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
    count = 0;
    }
    } else{
    System.out.println(mapNum.get(count+1)+" "+strNum.charAt(i)+(mapNum.get(count+1)!="one"? "s":""));
    }
    }

    }

    }
    This code will work for all the combinations

  11. #11
    Join Date
    Dec 2011
    Posts
    5

    Re: Java Challenge

    wow looks cool...i try using map and hashtable but i always get stuck wen comparing d charaters...thanks guy

  12. #12
    Join Date
    Dec 2011
    Posts
    4

    Re: Java Challenge

    thanks!!

  13. #13
    Join Date
    Dec 2011
    Posts
    5

    Unhappy Re: Java Challenge

    der is a runtime error....d code does not consider the last character of d string....if input string is 4423.....the output is two 4s, one 2..and the ends...instead of two 4s, one 2, one 3....anyone have idea on how to include d last character...in d counting

  14. #14
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Challenge

    @[email protected] Please don't post the answer to peoples homework, it benefits no-one.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  15. #15
    Join Date
    Dec 2011
    Posts
    4

    Re: Java Challenge

    @da_jokeer it worked fine for me..can u please recheck and let me know

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured