Jump to content

How to Set user Input to Int's and Doubles (Java)

Go to solution Solved by Guest,

Is it possible to do this:

import java.util.Scanner;

public class DamageCalc{

public static void main(String[] args){

Scanner s = new Scanner(System.in);

System.out.println("Please pokemon statistics");

int Type = s.nextInt();

int ATTACK = s.nextInt();

int Defense = s.nextInt();

int Base = s.nextInt();

int STAB = s.nextInt();

int Critical = s.nextInt();

double Random = s.nextDouble();

s.close();

}

}

 

Yes but your program will throw an exception and fail if one of those elements is missing or incorrectly entered.

Assignment:

http://www.cs.jhu.edu/~joanne/cs107/assign/pg02.shtml
This program will determine the damage done by a move between two battling Pokemon. Each Pokemon has a Type, Level, Attack, and Defense. In addition, each move has Base damage, Same-Type Attack Bonus, Critical, and Random modifiers.

Write a program that accepts as input these values and prints the resulting damage. Capitalization does not matter. Here is some example input and output:

input result
Type 2 level 5 Attack 80 Defense 60 Base 20 STAB 1 Critical 2.0 Random 0.9 Damage is 14
Type 1 Level 10 ATTACK 100 Defense 60 Base 40 STAB 1 Critical 1 Random 1 Damage is 10
Type 2 level 10 this is too much typing Expected attack!
Type 17 Level 10 ATTACK 100 Defense 60 Base 40 STAB 1 Critical 1 Random 1 Invalid type value!
Type 1 Level 10 ATTACK 100 Defense 60 Base 40 STAB 1 Critical 3 Random 1 Invalid critical value!
Note that the damage result is an integer and values are rounded down at the end of the calculation. Here are the damage formulas (stolen from Bulbapedia) which you must translate into code:

http://bulbapedia.bulbagarden.net/wiki/File:DamageCalc.png

Here are the rules regarding valid values for each of the different components:

Level is the level of the attacking Pokemon. It must be in the range [1, 99] inclusive.
Attack and Defense are the working Attack and Defense stats of the attacking and defending Pokemon, respectively. They must be greater than zero.
Base is the base damage of the attack. It must be greater than zero.
Modifier is calculated as follows:
look it up

STAB is the same-type attack bonus. This is equal to 1.5 if the attack is of the same type as the user, and 1 if otherwise.
Type is the type effectiveness. This can be either 0, 0.25, 0.5, 1, 2, or 4 depending on the type of attack and the type of the defending Pokemon.
Critical is 2 for a critical hit, 1 otherwise.
random is a random number from 0.85 to 1.00 inclusive (from the input).
other counts for things like held items. Assume that this value is always 1 in this assignment.
Name your class and file pg2b.

Implementation Details: Your challenge is to parse the input string and store each value, checking whether they are valid based on the rules above. You must check for each component of the input (case insensitive), but if it's there you can assume it is followed by a number. If any component name or number value is invalid the program should print an error and stop. If the input is verified then the program should compute the damage and print the result as an integer. Also, you are expected to use named constants (final) for all the values that are part of the problem statement, such as 250.

 

For example take this user input and set it up into individual Integers/Doubles:

Type 2 level 5 Attack 80 Defense 60 Base 20 STAB 1 Critical 2.0 Random 0.9 Damage is 14

 

I have researched the Scanner class with no luck. I know I have to use .nextInt(); and so on but don't know how to actually format it. I can't seem to find any examples either.

It would be great if somebody can pint me in the right direction. 

System: Thinkpad T460

 

Link to post
Share on other sites

Something like this, perhaps. I haven't compiled so don't just copy paste this code.
 

String input; // New string to store the inputScanner in = new Scanner(System.in); // New scanner pointed at System.ininput = in.nextLine(); // Read a single lineString[] parts = input.split(" "); // Split the input string on single spacesif (parts[0] == null) { // Empty input  System.err.println("Expected type"); // Print message to stderr  System.exit(1); // Exit with error code 1} else if (!isValidType(parts[0]) { // You would implement the method "boolean isValidType(String type)"  System.err.println("Invalid type");  System.exit(1);} else {  // The input was valid, do something with it}// And so on...
Link to post
Share on other sites

 

Something like this, perhaps. I haven't compiled so don't just copy paste this code.

 

String input; // New string to store the inputScanner in = new Scanner(System.in); // New scanner pointed at System.ininput = in.nextLine(); // Read a single lineString[] parts = input.split(" "); // Split the input string on single spacesif (parts[0] == null) { // Empty input  System.err.println("Expected type"); // Print message to stderr  System.exit(1); // Exit with error code 1} else if (!isValidType(parts[0]) { // You would implement the method "boolean isValidType(String type)"  System.err.println("Invalid type");  System.exit(1);} else {  // The input was valid, do something with it}// And so on...

 

Is it possible to do this:

import java.util.Scanner;

public class DamageCalc{

public static void main(String[] args){

Scanner s = new Scanner(System.in);

System.out.println("Please pokemon statistics");

int Type = s.nextInt();

int ATTACK = s.nextInt();

int Defense = s.nextInt();

int Base = s.nextInt();

int STAB = s.nextInt();

int Critical = s.nextInt();

double Random = s.nextDouble();

s.close();

}

}

System: Thinkpad T460

 

Link to post
Share on other sites

Is it possible to do this:

import java.util.Scanner;

public class DamageCalc{

public static void main(String[] args){

Scanner s = new Scanner(System.in);

System.out.println("Please pokemon statistics");

int Type = s.nextInt();

int ATTACK = s.nextInt();

int Defense = s.nextInt();

int Base = s.nextInt();

int STAB = s.nextInt();

int Critical = s.nextInt();

double Random = s.nextDouble();

s.close();

}

}

 

Yes but your program will throw an exception and fail if one of those elements is missing or incorrectly entered.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×