0

first of all - I'm totaly new in this kind of area, so sorry if this code looks confused.

I'm trying to make "program" that will show me my future subscribe count. So program ask me three questions -

  1. Whats ur actual subscribe number.
  2. How many subs do I get per day - rate.
  3. How many days

After that it would give me answer -

"In 22 days, u will have 2000 subs"

Its working fine when I type rate without decimal number such as 1,2,.. But when I type rate with decimal number, it throws me an error like 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.nextDouble(Unknown Source)
at test.maintest.main(maintest.java:16)

Code:

main.class

   package test;

import java.util.Scanner;
import java.util.Locale;

public class maintest {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in).useLocale(Locale.US);
        second classNd = new second();

        System.out.println("Your actual sub count is? ");
        Double count = input.nextDouble();
        System.out.println("How many days? ");
        Double day = input.nextDouble();
        System.out.println("Your actual rate ");
        Double rate = input.nextDouble();

        String answerCount = Double.toString(count);
        String answerDay = Double.toString(day);
        String answerRate = Double.toString(rate);

        classNd.getSubs(answerCount);
        classNd.getDay(answerDay);
        classNd.getRate(answerRate);
        classNd.answerMe();


        }

        }       

second.class:

package test;
public class second {

    private String gotSubs;
    public void getSubs(String answ) {
        gotSubs=answ;
    }   
    private String gotRate;
    public void getRate(String gtRate) {
        gotRate=gtRate;
    }
    private String gotDay;
    public void getDay(String gotDays){
        gotDay=gotDays;
    }
    public void answerMe() {

        double getRates = Double.parseDouble(gotRate);
        double amount;
        double numberSubs = Double.parseDouble(gotSubs);
        double numberOf = Double.parseDouble(gotDay);

        do{

            amount = numberSubs*Math.pow(1+getRates, numberOf);
            System.out.println("In " + numberOf + " days, u will have " + amount + " subs");
        }while(numberOf<0);
                }
        }

So I had to add Locale thing in to main class:

 import java.util.Scanner;
 import java.util.Locale;

    public class maintest {

        public static void main(String args[]) {
            Scanner input = new Scanner(System.in).useLocale(Locale.US);
            second classNd = new second();
Brego
  • 1
  • 2
  • Why did you accept answer which doesn't actually solve problem described by question? Improvement suggested there was to change `Double` to `double`. While it make sense, it isn't even remotely close to `java.util.InputMismatchException` which this question is all about. – Pshemo Mar 03 '18 at 14:01
  • Aside from that, methods like `public void getRate(String gtRate) { gotRate=gtRate; }` are not getters (which allows us to get/return named value like `int age = person.getAge()`), but setters - where you set named value (like `person.setAge(23);`). Also why are you using String type for numeric data? If value represents double and you want to treat it like that then instead of `private String gotSubs;` you should have `private int gotSubs;` (since subs are whole numbers so `int` is better than `double` which was meant to store floating point numbers). – Pshemo Mar 03 '18 at 14:09
  • Ye, like I said - I'm totaly new, but thanks - I didnt know that I can use `private int gotSubs` instead of `private String gotSubs`, so thank you. – Brego Mar 03 '18 at 14:35

0 Answers0