I'm whipping together a simple Java program to explain loops. I want each demonstration in a separate function. Right now, each function works fine, but only when the other isn't called. If I call both I get the following errors at run-time:
Please input a positive integer as the end value: 5
The summation is: 9
How many rows do you want your triangle to be?: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at loops.exercise2(loops.java:48)
at loops.main(loops.java:11)
Code:
import java.util.Scanner;
public class loops
{
public static void main( String args[] )
{
exercise1();
System.out.println();
exercise2();
}
public static void exercise1()
{
int limit;
int i;
int sum;
Scanner keyboard = new Scanner(System.in);
System.out.print ("Please input a positive integer as the end value: ");
limit = keyboard.nextInt();
i=1;
sum = 0;
while (i <= limit)
{
sum = sum + i;
i = i + 2;
}
System.out.print("The summation is: " + sum);
keyboard.close();
}
public static void exercise2()
{
int numRows, i, j;
Scanner keyboard = new Scanner(System.in);
System.out.print("How many rows do you want your triangle to be?: ");
numRows = keyboard.nextInt();
for(i=0; i<numRows; i++)
{
for(j=0; j<=i; j++)
{
System.out.print("*");
}
System.out.println();
}
keyboard.close();
}
}
This is happening because when you close your Scanner, it also closes the input stream, which in this case is System.in. When you try to instantiate the Scanner in your execise2 method, the input stream is closed.
See this SO post...
https://stackoverflow.com/a/13042296/1246574
My guess is your Scanner classes are interfering with each other. exercise1 takes input from standard in then closes when it's done. Then exercise2 also tries to get input from standard in which is closed.
I would suggest you only make 1 Scanner and pass it as a parameter to both exercise1 and exercise2 and then close it after both calls.
Make a global scanner, and initiate it only once, then call its methods.
Try not calling keyboard.close();
Related
I wrote a code which calculates grades. But if I'm typing 9999 in the console, then the program should break without any output. How can I do this and which loop should I use? I tried it with a while loop but the program gives me still output.. this is my code with the while loop which doesn't work as it should. The Programm works except for the while loop. How can I do write this better?
import java.util.Scanner;
public class average {
public static double average (double [] grade ){
double sum = 0;
int number = grade.length;
for(int i = 0; i<grade.length; i++){
sum+=grade[i];
}
double average = sum / number;
return average;
}
public static void main (String [] args){
Scanner s = new Scanner(System.in);
System.out.println("How much grades you add?");
int number = s.nextInt();
while(number == 9999){
break;
}
double [] grade = new double [number];
System.out.println("Please enter : ");
for(int i = 0; i<grade.length; i++){
grade[i] = s.nextDouble();
}
System.out.println("My grades are: ");
for(int i = 0; i<grade.length; i++){
System.out.println(grade[i] + " | ");
}
System.out.println("");
System.out.println("My average: " +average(grade));
}
}
You are using a break, which immediately exits only the loop. If you want to quit the program, you should use if and return like this:
if(number == 9999) {
return;
}
This quits the program because with return, you exit the current function. The current function is main(), that's the program's main code. So, if you exit it, you will quit the program.
In functions with a return value (non-void functions) you need to specify the return value like this:
return 9999;
If you are on an other program thread, you need to call System.exit(0).
You don't need to break any loop, you just need to exit the program.
if (number == 9999) {
System.exit();
}
I'm trying to get input in both my main and other methods, but I'm not clear on how to get the scanner working in both.
It gives me a weird error:
Exception in thread "main"
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Hwk11.getRainData(Hwk11.java: 28)
at Hwk11.main(Hwk11.java: 18)
Code:
import java.util.Scanner;
public class Hwk11 {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("How many weeks of data do you have?");
int numWeeks = stdin.nextInt();
if (numWeeks <= 0 || numWeeks > 52) {
System.out.println("Invalid number of weeks.");
}
else {
double[] rainWeeks = new double [numWeeks];
getRainData(rainWeeks);
showRain(rainWeeks);
}
}
public static void getRainData(double[] rainFall) {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter the weekly rainfall for each week.");
for (int index = 0; index < rainFall.length; index++) {
System.out.println("Week number " + (index + 1) + ":");
rainFall[index] = stdin.nextDouble();
}
}
public static void showRain(double[] rainFall) {
for (int index = 0; index < rainFall.length; index++) {
System.out.print(rainFall[index] + " ");
}
}
}
People are saying "works for me".
The problem that the behavior (whether it works or not) depends on exactly how input is provided.
If you provide input interactively, it will probably work.
If you provide input by redirecting standard input like this:
java Hwk11 < input.txt
then it won't.
The problem is that a Scanner will read-ahead and buffer any characters available from its input stream. That is fine normally, but in your code you have created two distinct Scanner objects to read from System.in. Thus when standard input is redirected:
The first Scanner.nextInt call will cause most / all of the input to be buffered in the first Scanner
When the second Scanner is created and Scanner.nextDouble is called, it won't see the input buffered in the first Scanner and that will lead to an exception ... when it runs out of input characters "too soon".
The solution is to NOT create multiple Scanner objects for the same input stream. Use one Scanner and either put it in a field, or pass it as a parameter to all of the places that it needs to be used.
Works for me. Don't enter anything but a double if you asking for next double so no "2.3 cm", just pass 2.3 and add cm when you print in
I am assuming your showRain(double[] rainFall) method probably executes before getRaindata(double[] rainFall) is able to finish and populate the array. Array you passsing to showRain(double[] rainFall) might be empty.
Try putting your method call for showRain(double[] rainFall) after loop in getRanData(double[] rainFall)
Alternitivly try passing the whole Scanner object to method.
public static void getRainData(double[] rainFall, Scanner stdin) {
//Scanner stdin = new Scanner(System.in);
System.out.println("Enter the weekly rainfall for each week.");
for (int index = 0; index < rainFall.length; index++) {
System.out.println("Week number " + (index + 1) + ":");
rainFall[index] = stdin.nextDouble();
}
showRain(rainFall);
}
Don't forget to close it when you done with scanner.
I'm trying to make a small program to help with my homework. It needs to square a number that is input and then sum all the squares and display them.
I came up with this but I have been getting errors.
import java.util.Scanner;
public class Statistics {
public static void main(String[] args) {
int sum = 0;
Scanner scan = new Scanner(System.in);
Scanner input = new Scanner(System.in);
System.out.println("\nHow many times? ");
int times = scan.nextInt();
scan.close();
for(int i = 0; i < times; i++) {
System.out.println("\nEnter the number to be squared: ");
int squ = input.nextInt();
System.out.println(squ + "\n\n");
sum += squ;
}
input.close();
System.out.println("\n\nSum = " + sum);
}
}
Here's the error I've been getting in the Eclipse console:
> Exception in thread "main" java.util.NoSuchElementException
> at java.util.Scanner.throwFor(Unknown Source)
> at java.util.Scanner.next(Unknown Source)
> at java.util.Scanner.nextInt(Unknown Source)
> at java.util.Scanner.nextInt(Unknown Source)
> at Statistics.main(Statistics.java:16)
Any help would be greatly appreciated.
When you close a Stream, it closes the underlying stream as well.
You almost never need to close System.in and I don't suggest you do it in this case.
You should avoid wrapping the same stream more than once, unless you like confusion. Unfortunately there is no simple way to prevent a developer from doing this but it is almost always a bug.
In short, you don't need to wrap System.in stream twice and in fact it will fix your problem.
This is how it might be written
Scanner scan = new Scanner(System.in);
System.out.println("\nHow many times? ");
int times = scan.nextInt();
double sum = 0;
for(int i = 0; i < times; i++) {
System.out.println("\nEnter the number to be squared: ");
double d = input.nextDouble();
double squ = d * d;
System.out.println(squ + "\n\n");
sum += squ;
}
System.out.println("\n\nSum = " + sum);
Use only one scanner, you don't need a second one.
Something like this:
import java.util.Scanner;
public class Statistics {
public static void main(String[] args) {
int sum = 0;
Scanner scan = new Scanner(System.in);
System.out.println("\nHow many times? ");
int times = scan.nextInt();
for(int i = 0; i < times; i++) {
System.out.println("\nEnter the number to be squared: ");
int squ = scan.nextInt();
System.out.println(squ + "\n\n");
sum += squ;
}
scan.close();
System.out.println("\n\nSum = " + sum);
}
}
Put the close of your first scanner to the close to the second one after for-loop.
Thats because your input-stream is closed with the first close and the second scanner cannot work.
I'm in the process of creating a program that reads data from an external file, compares it with other data within the file then prints the results to a new external file. I am having problems with the while loop section of my code. I am unsure whether it is the while loop itself or the for loop which is nested within. Here is the code:
public class WageCalculator {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new FileReader("TestData.txt")); //Scanner for external file
PrintWriter output = new PrintWriter("wagedaily.txt");
float RecommendedMaximum;
RecommendedMaximum = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter the recommended maximum journey cost:"));
String ShipID, JourneyID; //Variables
int JourneyLength, Crew;
double RateOfPay, CrewCost, TotalCost;
while (input.hasNext()) { //EOF-Controlled While Loop
ShipID = input.nextLine();
JourneyID = input.nextLine();
JourneyLength = input.nextInt();
Crew = input.nextInt();
CrewCost = 0; //Default Values Set
TotalCost = 0;
for (int x = Crew; x > 0; x--) { //For Loop updates the above values
RateOfPay = input.nextDouble();
CrewCost = RateOfPay * JourneyLength;
TotalCost = TotalCost + CrewCost;
}
if (TotalCost < RecommendedMaximum) { //if-else statements to compare values
System.out.println("The total cost of...");
output.println("The total cost of...");
} else if (TotalCost == RecommendedMaximum) {
System.out.println("The total cost of...");
output.println("The total cost of...");
} else {
System.out.println("The total cost of...");
}
}
output.close(); //Close both Scanner and Printwriter
input.close();
}
}
The error I get is this:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at (package).WageCalculator.main(WageCalculator.java:30)
The error says it's line 30 in my code that is the problem but I am not so sure.
Incase anyone needs to see the TestData.txt file:
Monarch //ShipID
M141 //JourneyID
16 //JourneyLength
6 //Crew
10.5 //RateOfPay -
10.5
20
20
20
30 //- RateOfPay
Princess //ShipID
P103 //JourneyID
18 //JourneyLength
5 //Crew
40 //RateOfPay -
45
45
60
80 //- RateOfPay
Any help would be appreciated :)
You're making a bunch of input.nextXXX() calls within the loop after checking input.hasNext() only once at the top of the loop. Don't do that as this is very unsafe and bound to fail and as there should always be a one-to-one correspondence between a check and a get. For instance, if you want to get a next line, there should be one input.HasNextLine() called before calling calling input.nextLine(). Same for input.next() or input.nextInt().
Myself, I'd read line by line by checking hasNextLine() and then once reading in the nextLine(), and then manipulating the String received.
while (input.hasNextLine()) {
String line = input.nextLine();
// now do not use input further within the loop
}
You could then within the loop use a second Scanner using the line received, or split the line via String#split(String regex) or do whatever you need to do with it.
Or you could use String replaceAll(...) and regular expressions to get rid of all white space followed by "//" followed by any chars. e.g.,
while (input.hasNextLine()) {
String line = input.nextLine();
// get rid of white space followed by "//" followed by anything
line = line.replaceAll("\\s+//.*", "");
System.out.println(line);
}
Edit
I've looked a bit more into your question and your data, and I am going to amend my answer. If you're absolutely sure of the integrity of your data file, you could consider checking for nextline once per obtaining data of an entire ship. For example:
public static void main(String[] args) {
InputStream inStream = GetData.class.getResourceAsStream(DATA_FILE);
List<Cruise> cruiseList = new ArrayList<>();
Scanner input = new Scanner(inStream);
while (input.hasNext()) {
String name = getLine(input);
String id = getLine(input);
int length = Integer.parseInt(getLine(input));
int crew = Integer.parseInt(getLine(input));
// assuming a class called Cruise
Cruise cruise = new Cruise(name, id, length, crew);
for (int i = 0; i < crew; i++) {
cruise.addPayRate(Double.parseDouble(getLine(input)));
}
cruiseList.add(cruise);
}
input.close();
}
private static String getLine(Scanner input) {
String line = input.nextLine();
// get rid of white space followed by "//" followed by anything
line = line.replaceAll("\\s+//.*", "");
return line;
}
The reason it gives you trouble is because when the user enters an integer then hits enter, two things have just been entered - the integer and a "newline" which is \n. The method you are calling, "nextInt", only reads in the integer, which leaves the newline in the input stream. But calling nextLine() does read in newlines, which is why you had to call nextLine() before your code would work. You could have also called next(), which would also have read in the newline.
Also read the Documentation of Scanner class for further understanding :
Here is the Corrected Code :
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class WageCalculator {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new FileReader("TestData.txt")); //Scanner for external file
PrintWriter output = new PrintWriter("wagedaily.txt");
float RecommendedMaximum;
RecommendedMaximum = Float.parseFloat(JOptionPane.showInputDialog(null,
"Enter the recommended maximum journey cost:"));
String ShipID, JourneyID; //Variables
int JourneyLength, Crew;
double RateOfPay, CrewCost, TotalCost;
while (input.hasNext()) { //EOF-Controlled While Loop
System.out.println("While Enter"); // For debugging purpose
ShipID = input.nextLine();
JourneyID = input.nextLine();
JourneyLength = input.nextInt();
input.nextLine(); // Enter this to read the data of skipped Line
Crew = input.nextInt();
input.nextLine();
CrewCost = 0; //Default Values Set
TotalCost = 0;
for (int x = Crew; x > 0; x--) { //For Loop updates the above values
System.out.println("While Under if Enter");// For debugging purpose
RateOfPay = input.nextDouble();
input.nextLine();
CrewCost = RateOfPay * JourneyLength;
TotalCost = TotalCost + CrewCost;
System.out.println("While Under if Exit");// For debugging purpose
}
if (TotalCost < RecommendedMaximum) { //if-else statements to compare values
output.println("The total cost of...");
} else if (TotalCost == RecommendedMaximum) {
System.out.println("The total cost of...");
output.println("The total cost of...");
} else {
System.out.println("The total cost of...");
}
System.out.println("While Exit"); // For debugging purpose
}
output.close(); //Close both Scanner and Printwriter
input.close();
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
import java.util.Scanner;
public class TestEditor {
public static void main(String[] args){
String input;
char[] words = new char[100];
int choice=0;
int start=0;
int end=0;
LineEditor myEditor = new LineEditor();
LineEditor myEditor2 = new LineEditor();
String input2 = null;
System.out.println("+++++++ LineEditor starts... +++++++\n");
System.out.println("* Write the text you want (maximum length: 100): ");
Scanner in = new Scanner(System.in);
input = in.next();
while(input.length()>100){
System.out.println("* Operation failed: You exceeded the maximum length.");
System.out.println("* Write the text you want (maximum length: 100): ");
input = in.next();
System.out.println("\n");
}
System.out.println("--------------------------------------\n");
do{
System.out.println("*Choose the menu:\n1. Insert\n2. Delete\n3. Replace\n4. Quit");
choice=in.nextInt();
System.out.println("\n");
if(choice==1){
System.out.println("* Enter the starting position:");
start = in.nextInt();
System.out.println("* Enter the text you want to replace:");
input2=in.next();
myEditor.insert(input, start, input2);
}
if(choice ==2){
System.out.println("* Enter the starting and ending position for deletion.");
start=in.nextInt();
end=in.nextInt();
myEditor.delete_text(input, start,end);
}
if(choice==3){
System.out.println("* Enter the starting and ending position for insertion.");
start=in.nextInt();
end=in.nextInt();
System.out.println("* Enter the text you want to replace:");
input2=in.next();
myEditor.replace(input, input2, start, end);
}
}while(choice !=4);
System.out.println("Good Bye!");
}
}
public class LineEditor {
private static char [] text;
private static char [] text2;
public LineEditor(){
text=new char[100];
text2=new char[100];
}
public void insert(String input, int start, String input2){
start = start-1;
int j=0;
for(int i=0;i<input.length();i++){
text[i]=input.charAt(i);
}
for(int i=0; i<input2.length();i++){
text2[i]=input2.charAt(i);
}
for(int i=start; i<input.length();i++){
text[i]=text[i+start];
}
for(int i=start; i<input.length();i++){
text[i]=text2[j];
j++;
}
for(int i=0; i<text.length;i++){
System.out.print(text[i]);
}
}
public void delete_text(String input, int start, int end){
for(int i=0;i<input.length();i++){
text[i]=input.charAt(i);
}
start=start-1;
int num = end-start;
for(int i=start; i<end;i++){
text[i]=text[i+num];
}
for(int i=end;i<text.length;i++){
if((i+end)<100){
text[i]=text[i+end];
}else{
text[i]=text[i-end];
}
}
for(int i=0; i<text.length;i++){
System.out.print(text[i]);
}
}
public void replace(String input, String input2, int start, int end){
start = start-1;
int j=0;
for(int i=0;i<input.length();i++){
text[i]=input.charAt(i);
}
for(int i=0; i<input2.length();i++){
text2[i]=input2.charAt(i);
}
for(int i=start; i<input.length();i++){
text[i]=text[i+start];
}
for(int i=start; i<input.length();i++){
text[i]=text2[j];
j++;
}
for(int i=0; i<text.length;i++){
System.out.print(text[i]);
}
}
}
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at TestEditor.main(TestEditor.java:29)
So those are my two classes and I keep getting this error when I put a space in my initial input. The error doesn't even happen when I transfer it to an array. Can someone shed some light? Also here is a link for the exact prompt if you would like to look at it.
Rather than in.next(), try using in.nextLine() instead. See if that makes a difference.
Replace next() to nextLine()
next() takes space as delimiter, so when you enter e.g. "Hello test", it take "Hello" only as first input, remaining string goes as input for next scanner call ( in your case this is choice=in.nextInt();) and it will fail to parse string to int.
From Docs:
public class InputMismatchException
extends NoSuchElementException
Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
Change next() to nextLine()
next() can read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.
nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.
Use the nextLine() method to read all of what is on the current line. It is possible for the current line to only have a newline character which would return an empty string.