0

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);
}
}
3
  • Without looking at your code, but only reading the top part of your question, you might be looking for something like String.valueOf(someInt). This way you can accept input as ints, but then convert them to strings. Commented Apr 23, 2014 at 17:19
  • just use baseball.next() Commented Apr 23, 2014 at 17:20
  • azurefrog- it erros out because all of my variables are ints I would have to change them to strings Commented Apr 23, 2014 at 17:22

4 Answers 4

1

You can simply use

int yourValue;
String yourStringFromTheValue = yourValue+"";
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

You can just do the following: String str = theint + "";

or baseball.next() to get the string from Scanner

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.