Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# Java function

### #1 jaj2 Posted 25 January 2006 - 02:24 PM

jaj2

New Member

• Member
• 5 posts
I have to write a function that prints the sum of all the integers from 1 to 1000 that don't contain the digit 7 (e.g. 48933 does not contain a 7, but 443973 does).
can anyone help
thanks
• 0

### #2 bdlt Posted 25 January 2006 - 06:22 PM

bdlt

Member

• Member
• 876 posts
try this:

```private void sum() {
int sum  = 0;
String s = "";
for( int i = 1;i < 1000;i++ ) {
sum += i;
s = " " + sum;
if( s.indexOf( '7' ) == -1 ) {
System.out.print( s );
}
}
```

• 0

### #3 jaj2 Posted 25 January 2006 - 08:51 PM

jaj2

New Member

• Topic Starter
• Member
• 5 posts
Thank you for the help, i really appreciate it.
cheers,

• 0

### #4 bdlt Posted 26 January 2006 - 06:58 AM

bdlt

Member

• Member
• 876 posts
you're welcome.

the for loop needs to be modified to include 1000

for( int i = 1;i <= 1000;i++ ) {
• 0

### #5 destin Posted 03 February 2006 - 10:28 PM

destin

Member

• Member
• 53 posts

try this:

```private void sum() {
int sum  = 0;
String s = "";
for( int i = 1;i < 1000;i++ ) {
sum += i;
s = " " + sum;
if( s.indexOf( '7' ) == -1 ) {
System.out.print( s );
}
}
```

I would like to point out that constantly rebuilding a String is not a good habit. Strings are immutable. Everytime you append to a String or reset the value of a String, new memory will be allocated for that String. It would be much better to use StringBuffer or StringBuilder (which are mutable classes). In this case, the best way to go about printing out the sum is simply System.out.print(" " + sum);. This saves us a variable, and saves us tons of memory allocation (the way you're doing, memory will be allocated for s 1000 times! [actually 999, but it was fixed in your other post]).

So, rewritten:
```public void sum() {
int sum = 0;
for (int i = 1; i <= 1000; i++) {
sum += i;
if (String.valueOf(sum).indexOf('7') == -1) {
System.out.print(sum + " ");
}
}
}```

Hope this helps. (oh... and you [bdlt] were missing a closing bracket too )

Edited by destin, 04 February 2006 - 11:32 AM.

• 0

### #6 bdlt Posted 04 February 2006 - 08:11 PM

bdlt

Member

• Member
• 876 posts

I would like to point out that constantly rebuilding a String is not a good habit.

destin has made a very good point regarding Strings. reassigning a new value to a String 1000 times is not sound java programming. thanks for pointing out acceptable alternatives.
• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users