Reputation: 21
I need to get the values as strings. How do I do this? In the first set of directions I was given seven numbers as integers. So I set all the variables up as integers besides players, but I also want to get all values as strings.
import java.util.Scanner;
public class Baseball {
public static void main(String[] args) {
//opens a new scanner instance
Scanner baseball = new Scanner(System.in);
// Allows a person to enter players name
System.out.print("Enter the Players Name: ");
// reads the users entry and assigns it the String "players"
String players = baseball.nextLine();
// Allows a person to enter the amount of at bats
System.out.print("Enter the amount of at bats: ");
// reads the users entry and assigns it the int "atBats"
int atBats = baseball.nextInt();
// Allows a person to enter home runs
System.out.print("Enter the amount of homeruns: ");
// reads the users entry and assigns it the int "homeruns"
int homeruns = baseball.nextInt();
// Allows a person to enter walks
System.out.print("Enter the amount of walks: ");
// reads the users entry and assigns it the int "walks"
int walks = baseball.nextInt();
// Allows a person to enter Doubles
System.out.print("Enter the amount of doubles: ");
// reads the users entry and assigns it the int "doubles"
int doubles = baseball.nextInt();
// Allows a person to enter Triples
System.out.print("Enter the amount of triples: ");
int triples = baseball.nextInt();
// Allows a person to enter hits
System.out.print("Enter the amount of hits: ");
int hits = baseball.nextInt();
baseball.close();
//calculates singles
int singles = (hits - homeruns - doubles - triples);
battingAverage(hits, atBats);
onBasePercentage(hits, atBats, walks);
sluggingPercentage(singles, doubles, triples, homeruns, atBats);
}
//method that calculates battingAverage
public static void battingAverage(int hits, int atBats) {
Double average = (double) (hits / atBats);
System.out.println("The batting average is: " + average);
}
//method that calculates onBasePercentage
public static void onBasePercentage(int hits, int atBats, int walks) {
Double basePercentage = (double) (hits + walks) / (atBats + walks);
System.out.println("The on base percentage is: " + basePercentage);
}
//method that calculates sluggingPercentage
public static void sluggingPercentage(int singles, int doubles,int triples, int homeruns, int atBats) {
Double slugPercentage = (double) (singles + 2 * doubles + 3 * triples + 4 * homeruns)/ atBats;
System.out.println("The slugging percentage is: " + slugPercentage);
}
}
Upvotes: 0
Views: 147
Reputation:
You can simply use
int yourValue;
String yourStringFromTheValue = yourValue+"";
Upvotes: 1
Reputation: 4252
You can use :
int val = 3 ;
String myStrVal = Integer.toString(val);
Similarly for double values you can use :
double dVal = 3.4;
String myStrVal = Double.toString(dVal);
Upvotes: 0
Reputation: 1507
You can just do the following: String str = theint + "";
or baseball.next() to get the string from Scanner
Upvotes: 0
Reputation: 15146
Integer.toString(int);
There is a class representing each primitive type. They contain tools. If you must grab them as Strings and convert to int:
Switch your scanner from nextInt()
to next()
, and use Integer.parseInt(string);
Upvotes: 0