PDA

View Full Version : The Java Strikes Back


Hotwheelz
03-20-2011, 05:21 PM
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.


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"
"<rt">
"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<string> 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<string> list = new ArrayList<string>();

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));
}

}

</string></string></string></rt">

RhymesayersDU
03-20-2011, 06:02 PM
http://www.codeproject.com/KB/vbscript/Steganography/homer2gifhta.gif

BroncosSR
03-20-2011, 06:18 PM
The debugger is your best friend...



public static String getString(ArrayList<String> 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;
}

Hotwheelz
03-20-2011, 06:26 PM
The debugger is your best friend...



public static String getString(ArrayList<String> 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 tits, I'd show them to you.

Fedaykin
03-20-2011, 07:04 PM
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:


public static String getString( List<String> 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".

AZorange1
03-20-2011, 10:14 PM
The debugger is your best friend...



public static String getString(ArrayList<String> 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. :welcome:

Hotwheelz
03-20-2011, 10:21 PM
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:


public static String getString( List<String> 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.

Fedaykin
03-20-2011, 11:01 PM
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.