How do I fix my "java.util.InputMismatchException" error? -


all need loop again user can continuously use program if to. let me know if there reference can read to, me understand more problem. in advance.

import java.util.scanner;  public class module3assignment1 {      // public variables     public static string letterchosen;     public static int loop = 0;     public static double radius, area;     public static scanner scanner = new scanner(system.in);      public static void main(string[] args) {          // tells user program         system.out.println("welcome round object calculator");         system.out.println("this program calculate area of circle of colume of sphere.");         system.out.println("the calculations based on user input radius.");         system.out.println("");              // loops while user wants calculate information             while (loop == 0){                  input();                 system.out.print(answer());                  system.out.println("do want calculate round object (y/n)? ");                 string input = scanner.next().touppercase();                     if (input == "n"){                         loop = 1;                         }             }          // ending message/goodbye         goodbye();         scanner.close();      }      private static void input(){          // prompts user input         system.out.print("enter c circle or s sphere: ");         letterchosen = scanner.nextline().touppercase();         system.out.print("thank you. radius of circle (in inches): ");         radius = scanner.nextdouble();      }      private static double areacircle(){          // calculates area of circle         area = math.pi * math.pow(radius, 2);         return area;      }      private static double areasphere(){          // calculates area of sphere         area = (4/3) * (math.pi * math.pow(radius, 3));         return area;      }      private static string answer(){          //local variables         string answer;          if(letterchosen == "c"){             // builds string circle answer , sends             answer = string.format("%s %f %s %.3f %s %n", "the volume of circle radius of", radius, "inches is:", areacircle(), "inches");             return answer;         }else{             // builds string sphere answer , sends             answer = string.format("%s %f %s %.3f %s %n", "the volume of sphere radius of", radius, "inches is:", areasphere(), "cubic inches");             return answer;         }     }      private static string goodbye(){          // local variables         string goodbye;          // says , returns goodbye message         goodbye = string.format("%s", "thank using round object calculator. goodbye");         return goodbye;     }  } 

the below console output , error getting after execution

welcome round object calculator program calculate area of circle of colume of sphere. calculations based on user input radius.  enter c circle or s sphere: c thank you. radius of circle (in inches): 12 volume of sphere radius of 12.000000 inches is: 5428.672 cubic inches  want calculate round object (y/n)?  y enter c circle or s sphere: thank you. radius of circle (in inches): c exception in thread "main" java.util.inputmismatchexception     @ java.util.scanner.throwfor(scanner.java:840)     @ java.util.scanner.next(scanner.java:1461)     @ java.util.scanner.nextdouble(scanner.java:2387)     @ module3assignment1.input(module3assignment1.java:48)     @ module3assignment1.main(module3assignment1.java:24) 

import java.util.scanner;  public class module3assignment1 { // public static variables discouraged...  private static char letterchosen; //char takes less memory  private static char useagain = 'y'; //just use answer loop...  private static double radius, area; private static string answer; private  static scanner scanner = new scanner(system.in);   //you might want clear screen after user gave answer round object private static void clearscreen(){     for(int =0;i<50;i++){system.out.print("\n");} }  public void input(){      // prompts user input     system.out.print("enter c circle or s sphere: ");     letterchosen = scanner.next().charat(0);     system.out.print("thank you. radius of circle (in inches): ");     radius = scanner.nextdouble();     this.answer= answer(letterchosen);  }  public double areacircle(double radius){      // calculates area of circle     area = math.pi * math.pow(radius, 2);     return area;  }  public double areasphere(double radius){      // calculates area of sphere     area = (4/3) * (math.pi * math.pow(radius, 3));     return area;  }  public string answer(char letterchosen){      //local variables     string answer = "";     if(letterchosen=='c'||letterchosen=='c'){         answer = string.format("%s %f %s %.3f %s %n", "the volume of circle radius of", radius, "inches is:", areacircle(radius), "inches");     }else{         answer = string.format("%s %f %s %.3f %s %n", "the volume of sphere radius of", radius, "inches is:", areasphere(radius), "cubic inches");     }     return answer; }  private static string goodbye(){      // local variables     string goodbye;      // says , returns goodbye message     goodbye = string.format("%s", "thank using round object calculator. goodbye");     return goodbye; }  public static void main(string[] args) {      // tells user program     system.out.println("welcome round object calculator");     system.out.println("this program calculate area of circle of colume of sphere.");     system.out.println("the calculations based on user input radius.");     system.out.println("");     module3assignment1 ass1 = new module3assignment1();          // loops while user wants calculate round object         while (useagain == 'y'||useagain=='y'){              ass1.input();             system.out.print(answer);             system.out.println("do want calculate round object (y/n)? ");             useagain = scanner.next().charat(0);             system.out.println(useagain);             clearscreen();         }      // ending message/goodbye     system.out.println(goodbye());     scanner.close();  } 

}

some things changed:

  • i used char instead of string. string takes more memory char.
  • added clearscreen() method "clears" screen when you're using console.
  • i added parameter radius areasphere , areacircle methods. makes methods reusable.

  • i changed public static variables private static. using public static variables highly discouraged. may read this find out why.

  • and prevent public static variables, created instance of module3assignment1 instead of having in static.

  • changed casing of method names. please follow camel-casing, means first letter of method lowercase , other words have first letter in uppercase (e.g. input(), areasphere() )

a comment comparing strings:

== compares references object , not values

use .equals() or .equalsignorecase() if want compare values of 2 strings. here sample syntax:

if(string1.equals(string2)){ //do } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -