I'm trying to write a program in Java that takes 3 names and integers, averages and displays them

3026 views java
7

I'm trying to write a program in Java that prompts and reads the users name, three integers, then calculates the total and average, and outputs the name, three scores, total, and average.

I know how to make an average using print but don't know how to implement the names into the program. this is what I have so far.

import java.util.Scanner;

public class Averages {

 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);

  System.out.print("Input first number: ");
  int num1 = in.nextInt();

  System.out.print("Input second number: ");
  int num2 = in.nextInt();

  System.out.print("Input third number: ");
  int num3 = in.nextInt();


  System.out.println("Average of 3 numbers is: " + 
   (num1 + num2 + num3) / 3);
 }
}

answered question

Have you tried String name = in.next()?

I'm pretty sure stackoverflow.com/questions/11871520/… answers your exact question. For future reference, I found that answer by making google find me "java read in string". When you get stuck, the first thing to do is google for "things that kind of sound like what you want to do". You'll have to look at 10 or so results, but that's fine: that's part of spending the effort to solve your problem.

1 Answer

9

Here's a sample code snippet

public class Averages {

 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);

  System.out.print("Enter user name: ");
  String name = in.nextLine();

  System.out.print("Input first number: ");
  int num1 = in.nextInt();

  System.out.print("Input second number: ");
  int num2 = in.nextInt();

  System.out.print("Input third number: ");
  int num3 = in.nextInt();

  System.out.print("User name is : " + name);
  System.out.print("Total of 3 numbers is : " + (num1 + num2 + num3));
  System.out.println("Average of 3 numbers is: " + 
   (num1 + num2 + num3) / 3);
 }
}

posted this

Have an answer?

JD

Please login first before posting an answer.