Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
  1  import java.util.Scanner;
  2  
  3  public class MyCalculator {
  4      public static void main(String[] args) {
  5          Scanner scanner = new Scanner(System.in);
  6          // userInput
  7  
  8          // i searched online how to restart a program with y/n
  9          while (true) {
 10              // this is to get it to loop continuously if the user says yes
 11  
 12              while (true) {
 13                  System.out.println("Enter the calculator mode: Standard or Scientific? ");
 14                  String mode = scanner.nextLine();
 15  
 16                  if (mode.equalsIgnoreCase("Scientific")) {
 17                      System.out.println("Enter '+' for addition, '-' for subtraction, '*' for multiplication, '/' for division");
 18                      // equalsIgnoreCase because often people will put words in upper or lower case unless specified so it's important to account for that
 19                      System.out.print(", 'sin' for sin x, 'cos' for cos x, 'tan' for tan x: ");
 20                  }
 21  
 22                  String operation = scanner.nextLine();
 23  
 24                  if (operation.equals("+") || operation.equals("-") || operation.equals("*") || operation.equals("/")) {
 25                      System.out.print("How many numbers do you want to " + operation + "? ");
 26                      // how many numbers do you want to +, -, *, /
 27                      int count = scanner.nextInt();
 28                      scanner.nextLine(); // consume the newline character left in the buffer
 29                      double result = 0.0;
 30                      // operation.equals just sees if it's equal to what I put and then executes the code, I put a bunch of
 31                      // or statements because it's different operations
 32  
 33                      System.out.print("Enter " + count + " numbers: ");
 34                      for (int i = 0; i < count; i++) {
 35                          double number = scanner.nextDouble();
 36                          scanner.nextLine(); // consume the newline character left in the buffer
 37                          // check this part, I'm not sure if I messed up by using an int and double
 38                          switch (operation) {
 39                              // switch cases are really useful when you have different options, I learnt this in programming methods and we did an example where we did a switch case with numbers and months
 40                              // we learnt this in class weeks ago where you can use a switch case
 41                              // we did an example with numbers and months
 42                              case "+":
 43                                  result += number;
 44                                  break;
 45                              // we also learnt that break; terminates loops and I typically
 46                              // make my loops infinite but in this case we don't want that
 47                              case "-":
 48                                  result -= number;
 49                                  break;
 50                              case "*":
 51                                  result *= number;
 52                                  break;
 53                              case "/":
 54                                  result /= number;
 55                                  break;
 56                          }
 57                      }
 58                      System.out.println("Result: " + result);
 59                  } else if (mode.equalsIgnoreCase("Scientific") && (operation.equalsIgnoreCase("sin") ||
 60                          // .equals ignore case because some people might put scientific or Scientific and I don't want the program to crash because of something so small
 61                          // && is an operator and if the userInput meets the condition then the code will execute
 62                          operation.equalsIgnoreCase("cos") || operation.equalsIgnoreCase("tan"))) {
 63                      System.out.print("Enter a number in radians to find the " + operation + ": ");
 64                      double number = scanner.nextDouble();
 65                      scanner.nextLine(); // consume the newline character left in the buffer
 66                      // radians should be double not int because it is decimals
 67                      double result;
 68  
 69                      switch (operation) {
 70                          // another switch case to help execute different options
 71                          case "sin":
 72                              result = Math.sin(number);
 73                              // returns the sine of the number, we learnt in class how to use math.random and math.sqrt and others
 74                              break;
 75                          case "cos":
 76                              result = Math.cos(number);
 77                              break;
 78                          case "tan":
 79                              result = Math.tan(number);
 80                              break;
 81                          default:
 82                              result = 0.0;
 83                              break;
 84                      }
 85                      System.out.println("Result: " + result);
 86                      // will produce result: and result(because of + result)
 87                  } else {
 88                      System.out.println("Invalid operator " + operation);
 89                      continue;
 90  
 91                  }
 92                  System.out.println("Do you want to start over? (Y/N)");
 93                  String response = scanner.next();
 94                  scanner.nextLine(); // consume the newline character left in the buffer
 95                  if ("Y".equalsIgnoreCase(response)) {
 96                      continue;
 97                  } else if ("N".equalsIgnoreCase(response)) {
 98                      System.out.println("Goodbye");
 99                      break;
100                  } else {
101                      System.out.println("Invalid input. Goodbye");
102                      break;
103                  }
104              }
105  
106              System.out.println("Do you want to start over? (Y/N)");
107              String response = scanner.next();
108              scanner.nextLine(); // consume the newline character left in the buffer
109              if ("Y".equalsIgnoreCase(response)) {
110                  continue;
111              } else if ("N".equalsIgnoreCase(response)) {
112                  System.out.println("Goodbye");
113                  break;
114              } else {
115                  System.out.println("Invalid input. Goodbye");
116                  break;
117              }
118          }
119  
120          scanner.close();
121      }
122  }


What I have tried:

I've tried fixing my scanner and rewriting the code
Posted
Updated 4-Apr-24 17:14pm
v2
Comments
Cindy Ann 4-Apr-24 21:11pm    
Sorry for not stating errors in title, My errors are:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at MyCalculator.main(MyCalculator.java:14)

This should help you identify the issue: How to Fix the No Such Element Exception in Java | Rollbar[^].
 
Share this answer
 
Start by looking at the error message closely - this will help: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]. It's primarily about compilation errors while your problem is a runtime error, but the information the error message is giving you is the same:
What Error
java.util.NoSuchElementException: No line found
Where Error
MyCalculator.main(MyCalculator.java:14)
So it's on line 14 of your code:
Java
14                  String mode = scanner.nextLine();
And it's saying "There is no next line".

So look at where the input is coming from:
Java
5          Scanner scanner = new Scanner(System.in);
It's the standard input: so it's either you typing an end-of-file marker, or you are piping a file into the input stream and it runs out of data when you expect to be able to continue.

The first is unlikely: you're working with a file of data, so you either need to look at the data - which we have no access to or any idea what the format is meant to be - or you need to run your code in the debugger to find out exactly where and why it doesn't match what you expected. Probably both would be a good idea!

Sorry, but we can't do that for you - you are going to have to look at your assignment again and check your handling of the data with the debugger.
 
Share this answer
 
To add to the advice given here - you can actually test to see if your scanner has any more input to work with by using the hasNext(); method to determine whether the scanner has anything else to do or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900