Jump to content

Welcome to Geeks to Go - Register now for FREE

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
Photo

Java function


  • Please log in to reply

#1
jaj2

jaj2

    New Member

  • Member
  • Pip
  • 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

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 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 );
	}
}

note: s.indexOf( '7' ) return -1 if '7' is not found
  • 0

#3
jaj2

jaj2

    New Member

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

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 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

destin

    Member

  • Member
  • PipPip
  • 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 );
	}
}

note: s.indexOf( '7' ) return -1 if '7' is not found

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 :tazz:)

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

  • 0

#6
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 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

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP