The Orange Mane -  a Denver Broncos Fan Community  

Go Back   The Orange Mane - a Denver Broncos Fan Community > Orange Mane Discussion > Orange Mane Central Discussion
Register FAQ Members List Calendar Chat Room Mark Forums Read



Reply
 
Thread Tools Display Modes
Old 03-20-2011, 05:21 PM   #1
Hotwheelz
Perennial Pro-bowler
 
Hotwheelz's Avatar
 

Join Date: Mar 2006
Posts: 844
Default The Java Strikes Back

The code explains itself. The problem is that instead of "Redtodaybook", I get "Redtoday;;lk". Why is it skipping "book" and why is it reading ";;lk"?

p.s. the Character.isLetter method yields the same result.


Code:
package homework7;


/*  Write the two methods described below.  Submit your assignment via
 * Blackboard.
 */

import java.util.ArrayList;


/* (Step 1) Write a method with the following header


 This method returns a String that is found by concatenating
 the Strings in the ArrayList object that begin with an
 alphabetic character.

 For instance if the ArrayList contains the following list of Strings

       "2"
       "cat"
       "
       "house"
       "???"
       "door"

  the method returns the String "cathousedoor"

   Suggestion: Declare a local String variable to store the String

   String tmp = "";

   The notes passed out in class on ArrayList objects contain an example that should
   help with this method.
 */
/*  (Step 2)

      Write a method with the header

      public static int getAlternatingSum(int[] arr)

      that returns the alternating sum of all elements in the array.
      For example, if arr contains the following values

       arr[0] = 3
       arr[1] = 18
       arr[2] = 100012
       arr[3] = 2
 *     arr[4] = 221

      then your method returns the following value

 *    3 - 18 + 100012 - 2 + 221
 
 */

public class Main {


   public static String getString(ArrayList c){
    int n = c.size();
    String tmp = "";
    char ch;
    int count = 0;


    for (int i = 0; i < n; i++){
       System.out.println("index = " + count + " counter = " + i + " String = " + tmp);
        tmp += c.get(i);
        ch = tmp.charAt(0);
        if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') && i < n){
              i++;
              System.out.println("index = " + count + " counter = " + i + " String = " + tmp);

        }
        
    System.out.println("index = " + count + " counter = " + i + " String = " + tmp);
    }
    return tmp;
   }

public static int getAlternatingSum(int[] arr)
    {
    int finalVal;
    int sum = 0;
    int difference = 0;

    for (int counter = 0; counter < arr.length; counter += 2)
        {
            sum += arr[counter];

        }
    for (int counter = 1; counter < arr.length; counter += 2)
        {
        difference -= arr[counter];
        }
    finalVal = sum - difference;

    return finalVal;

}

    public static void main(String[] args){

        ArrayList list  = new ArrayList();

        list.add("1");
        list.add("today");
        list.add("book");
        list.add(";;lk");
        list.add(0,"Red");
        list.add("+");

        String str = getString(list);

        int[] arr = new int[300];

        arr[0] = 1;
        arr[1] = -2;
        for(int k = 2; k < arr.length; k++){
            arr[k] = k + 1;
        }


        System.out.println("The string is " + str);
        System.out.println("The alternating sum is " + getAlternatingSum(arr));
    }

}
Hotwheelz is offline   Reply With Quote
Sponsored Links
Old 03-20-2011, 06:02 PM   #2
RhymesayersDU
Go Broncos, Nuggets, Rox
 
RhymesayersDU's Avatar
 

Join Date: Apr 2004
Location: Back In The 303!
Posts: 15,017

Adopt-a-Bronco:
Ty Lawson
Default

RhymesayersDU is offline   Reply With Quote
Old 03-20-2011, 06:18 PM   #3
BroncosSR
Perennial Pro-bowler
 

Join Date: Jan 2006
Location: Philadelphia, PA
Posts: 909
Default

The debugger is your best friend...


Code:
public static String getString(ArrayList c){
            int n = c.size();
            String tmp = "";
            char ch;
            int count = 0;


            for (int i = 0; i < n; i++){
               System.out.println("index = " + count + " counter = " + i + " String = " + tmp);
                
                ch = c.get(i).charAt(0);
                if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') && i < n){
                    tmp += c.get(i);
                      System.out.println("index = " + count + " counter = " + i + " String = " + tmp);

                }
                
            System.out.println("index = " + count + " counter = " + i + " String = " + tmp);
            }
            return tmp;
           }
BroncosSR is offline   Reply With Quote
Old 03-20-2011, 06:26 PM   #4
Hotwheelz
Perennial Pro-bowler
 
Hotwheelz's Avatar
 

Join Date: Mar 2006
Posts: 844
Default

Quote:
Originally Posted by BroncosSR View Post
The debugger is your best friend...


Code:
public static String getString(ArrayList c){
            int n = c.size();
            String tmp = "";
            char ch;
            int count = 0;


            for (int i = 0; i < n; i++){
               System.out.println("index = " + count + " counter = " + i + " String = " + tmp);
                
                ch = c.get(i).charAt(0);
                if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') && i < n){
                    tmp += c.get(i);
                      System.out.println("index = " + count + " counter = " + i + " String = " + tmp);

                }
                
            System.out.println("index = " + count + " counter = " + i + " String = " + tmp);
            }
            return tmp;
           }
If I had boobies, I'd show them to you.
Hotwheelz is offline   Reply With Quote
Old 03-20-2011, 07:04 PM   #5
Fedaykin
Ring of Famer
 

Join Date: Nov 2006
Posts: 5,100

Adopt-a-Bronco:
None
Default

As long as we're doing your homework, I figured we might as well help you learn something useful about programming! Take a look at the following:

Code:
public static String getString( List c ) {
    if ( c == null || c.size() == 0 ) {
        return "";
    } else {
        return (c.get(0).matches( "[a-zA-Z]*" ))
            ? c.get(0) + getString( c.subList(1, c.size() ) )
            : getString( c.subList(1,c.size()));
    }
}
Note: Copying this to your homework will get you an F for cheating as I find it highly unlikely your knowledge derived from this class includes the concepts used in that implementation.

Anyway, after NOT copying this, look up the following terms to understand this implementation:

* Regular Expressions
* Recursion
* Java Collections
* Trinary operator

and learn something useful to the real programming world =)

P.S. The BBS is stripping out some of the code. Look at the full source by clicking "quote".

Last edited by Fedaykin; 03-20-2011 at 07:07 PM..
Fedaykin is offline   Reply With Quote
Old 03-20-2011, 10:14 PM   #6
AZorange1
on borrowed time
 
AZorange1's Avatar
 
AZOrange1

Join Date: Jan 2010
Location: Phoenix, Az
Posts: 264

Adopt-a-Bronco:
Dennis Smith
Default

Quote:
Originally Posted by BroncosSR View Post
The debugger is your best friend...


Code:
public static String getString(ArrayList c){
            int n = c.size();
            String tmp = "";
            char ch;
            int count = 0;


            for (int i = 0; i < n; i++){
               System.out.println("index = " + count + " counter = " + i + " String = " + tmp);
                
                ch = c.get(i).charAt(0);
                if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') && i < n){
                    tmp += c.get(i);
                      System.out.println("index = " + count + " counter = " + i + " String = " + tmp);

                }
                
            System.out.println("index = " + count + " counter = " + i + " String = " + tmp);
            }
            return tmp;
           }
I understand totally ZERO about any of this BUT, I can celebrate because I HAVE 100 POSTS! Now I can contemplate something really catchy and cool to post.
AZorange1 is offline   Reply With Quote
Old 03-20-2011, 10:21 PM   #7
Hotwheelz
Perennial Pro-bowler
 
Hotwheelz's Avatar
 

Join Date: Mar 2006
Posts: 844
Default

Quote:
Originally Posted by Fedaykin View Post
As long as we're doing your homework, I figured we might as well help you learn something useful about programming! Take a look at the following:

Code:
public static String getString( List c ) {
    if ( c == null || c.size() == 0 ) {
        return "";
    } else {
        return (c.get(0).matches( "[a-zA-Z]*" ))
            ? c.get(0) + getString( c.subList(1, c.size() ) )
            : getString( c.subList(1,c.size()));
    }
}
Note: Copying this to your homework will get you an F for cheating as I find it highly unlikely your knowledge derived from this class includes the concepts used in that implementation.

Anyway, after NOT copying this, look up the following terms to understand this implementation:

* Regular Expressions
* Recursion
* Java Collections
* Trinary operator

and learn something useful to the real programming world =)

P.S. The BBS is stripping out some of the code. Look at the full source by clicking "quote".
Forgive me for asking for help after messing with it for hours. Dick.
Hotwheelz is offline   Reply With Quote
Old 03-20-2011, 11:01 PM   #8
Fedaykin
Ring of Famer
 

Join Date: Nov 2006
Posts: 5,100

Adopt-a-Bronco:
None
Default

Quote:
Originally Posted by Hotwheelz View Post
Forgive me for asking for help after messing with it for hours. Dick.
My apologies. My disdainful tone is not about you, but about the uselessness of the exercise they are making you do.
Fedaykin is offline   Reply With Quote
Reply

Thread Tools
Display Modes



Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Broncos want to be just like the Chiefs! Bob's your Information Minister Orange Mane Central Discussion 51 11-12-2010 04:22 PM
Great idea from one of your fellow Broncos fans... Bob's your Information Minister Orange Mane Central Discussion 6 11-03-2010 09:10 PM


All times are GMT -7. The time now is 01:42 AM.


Denver Broncos