12 Replies - 52353 Views - Last Post: 22 November 2008 - 08:32 AM Rate Topic: -----

#1 slugpr00f  Icon User is offline

  • New D.I.C Head

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

Java Help - Sum of Numbers Program

Post icon  Posted 22 October 2008 - 09:20 PM

Homework reads as :
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

please help ? :blink:

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  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • 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


Was This Post Helpful? 0
  • +
  • -

#3 slugpr00f  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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")

BufferedReader b = new BufferedReader(new FileReader(<input file>);
BufferedWriter w = new BufferedWriter(new FileWriter(<output file>);

String line = b.Readline()
while (line != null) {
w.Write(line.ToUpperCase());
line = b.ReadLine();
}

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



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

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Help - Sum of Numbers Program

Posted 22 October 2008 - 09:46 PM

View Postslugpr00f, 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")

BufferedReader b = new BufferedReader(new FileReader(<input file>);
BufferedWriter w = new BufferedWriter(new FileWriter(<output file>);

String line = b.Readline()
while (line != null) {
w.Write(line.ToUpperCase());
line = b.ReadLine();
}

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
Was This Post Helpful? 0
  • +
  • -

#5 slugpr00f  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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()
Was This Post Helpful? 0
  • +
  • -

#6 slugpr00f  Icon User is offline

  • New D.I.C Head

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

Re: Java Help - Sum of Numbers Program

Posted 22 October 2008 - 09:58 PM

View Postslugpr00f, 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();

BufferedReader b = new BufferedReader(new FileReader(<input file>);
BufferedWriter w = new BufferedWriter(new FileWriter(<output file>);

String line = b.Readline()
while (line != null) {
w.Write(line.toUpperCase());
line = b.ReadLine();
}

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




Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • 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();

BufferedReader b = new BufferedReader(new FileReader(fileIn);
BufferedWriter w = new BufferedWriter(new FileWriter(fileOut);



Was This Post Helpful? 0
  • +
  • -

#8 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • 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);
	}
}


Was This Post Helpful? 0
  • +
  • -

#10 marinabl  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 61
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 marinabl  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 61
  • Joined: 01-November 08

Re: Java Help - Sum of Numbers Program

Posted 20 November 2008 - 11:40 PM

View Postmarinabl, 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');
}
}
Was This Post Helpful? 0
  • +
  • -

#12 CherylP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 ; )
Was This Post Helpful? 0
  • +
  • -

#13 marinabl  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 61
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1