# Java Help - Sum of Numbers Program

Page 1 of 1

## 12 Replies - 128309 Views - Last Post: 22 November 2008 - 08:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=68695&amp;s=df6229d5cd4144db8f2c34baef2b83b8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 slugpr00f

Reputation: 0
• Posts: 8
• Joined: 22-October 08

# Java Help - Sum of Numbers Program

Posted 22 October 2008 - 09:20 PM

Sum of Numbers

Write a program that asks the user for a positive nonzero integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, … 50.

Sample output:

init:
deps-jar:
compile-single:
run-single:
Enter a positive nonzero number:
5
The sum of all the integers from 1 through 5 is 15
BUILD SUCCESSFUL (total time: 8 seconds)

so yea what i have is as follows but it wont compile, it tells me Syntax Error : A property cant go after this identifier and it highlights the first line

```public class sumofnumbers

{

}
system.out.println("Enter a positive number")
int n = n * (n+1) / 2
int i = 0

sum = 0;
for(int i = 0;i <= n; i++)
{
sum=sum+i;
}
System.out.println("Sum is"+sum);

```

Is This A Good Question/Topic? 0

## Replies To: Java Help - Sum of Numbers Program

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Java Help - Sum of Numbers Program

Posted 22 October 2008 - 09:32 PM

```import java.util.Scanner;

public class sumofnumbers
{

//	}  <---- no no your class does not finish here

// now you need a main method... this is the method that is executed when you java filename
public static void main(String[] arg) {
// System.out.println with a capital S
System.out.println("Enter a positive number");   // <--- missing; to end your statement
// now you have to read from the user
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();

// you have to declare sum before using it
int sum = 0;
for(int i = 0;i <= n; i++)
{
sum=sum+i;
}
System.out.println("Sum is " + sum);
}
}	// <---- here it is where the class finishes

```

### #3 slugpr00f

Reputation: 0
• Posts: 8
• Joined: 22-October 08

## Re: Java Help - Sum of Numbers Program

Posted 22 October 2008 - 09:41 PM

wow that was helpful. i have another one for you if thats ok?

HW is :

Uppercase File Converter

Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all the characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use Notepad or another text editor to create a simple file that can be used to test the program.

Sample of output:

init:

deps-jar:

compile-single:

run-single:

Enter the input file name:

TestFile.txt

Enter the output file name:

TestFile2.txt

BUILD SUCCESSFUL (total time: 11 seconds)

All the contents of the first file should have lower case letters and the contents of the next file should have everything in uppercase letters.

what i have so far is / it just wont compile or run...

```public class uppercasefilecnvt {

system.out.println("Uppercase File Converter")

BufferedWriter w = new BufferedWriter(new FileWriter(<output file>);

while (line != null) {
w.Write(line.ToUpperCase());
}

b.close();
w.close();

```

This post has been edited by slugpr00f: 22 October 2008 - 09:42 PM

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Java Help - Sum of Numbers Program

Posted 22 October 2008 - 09:46 PM

slugpr00f, on 22 Oct, 2008 - 09:41 PM, said:

wow that was helpful. i have another one for you if thats ok?

HW is :

Uppercase File Converter

Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all the characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use Notepad or another text editor to create a simple file that can be used to test the program.

All the contents of the first file should have lower case letters and the contents of the next file should have everything in uppercase letters.

what i have so far is / it just wont compile or run...

```public class uppercasefilecnvt {

system.out.println("Uppercase File Converter")

BufferedWriter w = new BufferedWriter(new FileWriter(<output file>);

while (line != null) {
w.Write(line.ToUpperCase());
}

b.close();
w.close();

```

You are not far...
the method is toUpperCase() with a t not a T
You will have to use a Scanner (like in the previous program) to input the 2 filenames from the user
instead of using scan.nextInt() which input an integer you will have to use scan.next() which returns a String (the filename)
the rest of your code looks OK to me

### #5 slugpr00f

Reputation: 0
• Posts: 8
• Joined: 22-October 08

## Re: Java Help - Sum of Numbers Program

Posted 22 October 2008 - 09:52 PM

so where would i put the scanner and scan.next()

### #6 slugpr00f

Reputation: 0
• Posts: 8
• Joined: 22-October 08

## Re: Java Help - Sum of Numbers Program

Posted 22 October 2008 - 09:58 PM

slugpr00f, on 22 Oct, 2008 - 09:52 PM, said:

so where would i put the scanner and scan.next()

wait like this?

```import java.util.Scanner;

public class uppercasefilecnvt {

system.out.println("Uppercase File Converter")

Scanner scan = new Scanner(system.in);
int scan.nextint();

BufferedWriter w = new BufferedWriter(new FileWriter(<output file>);

while (line != null) {
w.Write(line.toUpperCase());
}

b.close();
w.close();

```

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Java Help - Sum of Numbers Program

Posted 23 October 2008 - 04:12 AM

```Scanner scan = new Scanner(System.in);
System.out.print("Enter Input filename: ");
String fileIn = scan.next();
System.out.println("Enter output filename: ");
String fileOut = scan.next();

BufferedWriter w = new BufferedWriter(new FileWriter(fileOut);

```

### #8 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Java Help - Sum of Numbers Program

Posted 23 October 2008 - 04:24 AM

An alternative solution to the first problem with summing up to n..

int n = 50; //Just representing the user input.
for (int i = 0; i < 1; i++) {
int sum = (n * (n + 1)) / 2;
}

System.out.println(sum);

It's using a loop and that was the requirement, right?!

I notice it's in your code, the formula of an arithmetic sum. It's very useful in programming and math so remember it.

This post has been edited by Gloin: 23 October 2008 - 04:26 AM

### #9 g00se

• D.I.C Lover

Reputation: 3562
• Posts: 16,235
• Joined: 20-September 08

## Re: Java Help - Sum of Numbers Program

Posted 23 October 2008 - 04:32 AM

It's not really efficient to copy files using BufferedReader/Writer. Yes, i know you're not doing a pure copy, but it would still be more efficient to use FileReader/Writer and do something like:

```int buf = -1;
while((buf = in.read()) > -1) {
char c = (char)buf;
if (Character.isLetter(c)) {
out.write(Character.toUpperCase(c));
}
else {
out.write(c);
}
}

```

### #10 marinabl

Reputation: 1
• Posts: 65
• Joined: 01-November 08

## Re: Java Help - Sum of Numbers Program

Posted 20 November 2008 - 10:20 PM

Hello,
pardon me for asking but what if the user inputs in a value that is not a positive integer?
Shouldnt this be included in this code?
Also you might want to add the option to repeat function

just a thought

### #11 marinabl

Reputation: 1
• Posts: 65
• Joined: 01-November 08

## Re: Java Help - Sum of Numbers Program

Posted 20 November 2008 - 11:40 PM

marinabl, on 20 Nov, 2008 - 09:20 PM, said:

Hello,
pardon me for asking but what if the user inputs in a value that is not a positive integer?
Shouldnt this be included in this code?
Also you might want to add the option to repeat function

just a thought

I gave this a try and this is what I got, but I cant figure out the error, any suggestions?
[code]
public static void main(String[]arg)

{

char repeat;

System.out.println("Enter any positive number");

Scanner scan = new Scanner(System.in);
int num = scan.nextInt();

int sum = 0;
for(int x = 0;x <= num; x++)

{
sum=sum+x;

}
System.out.println("Sum is " + sum);

System.out.println("would you like to do this agian?");

System.out.print("Enter Y for yes and N for No:");

repeat = input.charAt(0);

}
while (repeat == 'Y'|| repeat == 'y');
}
}

### #12 CherylP

Reputation: 1
• Posts: 38
• Joined: 30-September 08

## Re: Java Help - Sum of Numbers Program

Posted 21 November 2008 - 12:06 PM

This guy has to be in my class...we have the same exact problems ; )

### #13 marinabl

Reputation: 1
• Posts: 65
• Joined: 01-November 08

## Re: Java Help - Sum of Numbers Program

Posted 22 November 2008 - 08:32 AM

Not the case, I have a library book ive been practicing with and i thought id giv eyour code a try and add a repeat loop, however it doesnt work, akkkkk