Yes, something is missing. The program is not complete. The braces do not balance. Here are my comments
public class Sum1 {
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
final int Sentinel = -1; // I would put SENTINEL
int i=0; // initialize to 0
int sum = 0;
System.out.println("Enter an integer:(-1 to start summation)");
while(i != Sentinel) { // 0!=SENTINEL
i = in.nextInt(); // put this in the loop
// note: this does not do any data validation.
// perhaps this will be covered later in your course
sum+=i;
} // end while
// print sum as the output of the summation
} // end main
} // end public class
Write a loop that will continuously prompt the user for integers to sum, -1 to exit. Sum these values and display the sum to the console.
Here is what I have so far, but I feel like I am missing something:
public class Sum1 {
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
final int Sentinel = -1;
int i;
int sum = 0;
System.out.println("Enter an integer:");
i = in.nextInt();
while(i != Sentinel) {
System.out.println("Enter an integer:");
i = in.nextInt();
sum = sum + i;
System.out.println("sum: " + sum + ".");
}
}
}
3 answers
Thanks for the help. I have another question: We have to open and read a file until the sentinel value of "xxx" appears. Also, we have to count the lines read not including the line with the sentinel value.
This is the file:
A gentle breeze rustling the dry cornstalks.
A sound is heard, a goblin walks.
A harvest moon suffers a black cat'scry.
Oh' do the witches fly!
Bonfire catches a pumpkins gleem.
Rejoice, it's Halloween!
xxx
-Richard Anderson (me) © Copyright 1998
This is what I have written:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Text1 {
public static void main(String[] args) throws IOException {
String line = null;
int linecount = 0;
BufferedReader br = new BufferedReader(new FileReader("input1.txt"));
while((line = br.readLine()) != null && (line = br.readLine()) != "xxx"){
linecount ++;
}
System.out.println("Number of lines in file is: " + linecount + ".");
}
}
This is the file:
A gentle breeze rustling the dry cornstalks.
A sound is heard, a goblin walks.
A harvest moon suffers a black cat'scry.
Oh' do the witches fly!
Bonfire catches a pumpkins gleem.
Rejoice, it's Halloween!
xxx
-Richard Anderson (me) © Copyright 1998
This is what I have written:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Text1 {
public static void main(String[] args) throws IOException {
String line = null;
int linecount = 0;
BufferedReader br = new BufferedReader(new FileReader("input1.txt"));
while((line = br.readLine()) != null && (line = br.readLine()) != "xxx"){
linecount ++;
}
System.out.println("Number of lines in file is: " + linecount + ".");
}
}
Actually the solution to this problem is not that much different from the previous one.
You would open a scanner to read from a file, which would be
Scanner in=new Scanner(new File("filename");
Declare and initialize an integer counter to zero.
Start the while loop, but the pretest condition will be in.hasNextLine(), since you're trying to read in one line at a time.
You'll actually read the line with
String line=in.nextLine();
Check if xxx is in the line using
the condition line.matches("xxx").
If that happens, break from the while loop, otherwise increment counter.
Outside of the while loop, print the number of lines as required.
Make a test and try your best to compile and run it, it's works out well. I got 6 lines for the answer.
You would open a scanner to read from a file, which would be
Scanner in=new Scanner(new File("filename");
Declare and initialize an integer counter to zero.
Start the while loop, but the pretest condition will be in.hasNextLine(), since you're trying to read in one line at a time.
You'll actually read the line with
String line=in.nextLine();
Check if xxx is in the line using
the condition line.matches("xxx").
If that happens, break from the while loop, otherwise increment counter.
Outside of the while loop, print the number of lines as required.
Make a test and try your best to compile and run it, it's works out well. I got 6 lines for the answer.