I'm a beginner.....I cant seem to solve this problem involving multiple methods....the method using the scanner isn't doing anything
package bucky;
import java.util.Scanner;
public class test {
public static void main(String args[]){
input();
for(int time=1;time<values[2];++time){
double A=values[1]*Math.pow(1+values[3], time);
System.out.println("You have: "+A+ " subscribers today");
}}
public static double[] input(){
Scanner s=new Scanner(System.in);
double[] values=new double[3];
System.out.println("Enter the Principal value: ");
values[1]=s.nextDouble();
System.out.println("Enter the no. of days: ");
values[2]=s.nextInt();
System.out.println("Enter the Rate of growth : ");
values[3]=s.nextDouble();
return values;
}
}
input() has to be assigned to values right? Like: double[] values = input();
Also, the indexing in array starts with 0... so values[0] is the first value and so on.
Try this code:
public static void main(String args[])
{
double[] values = input();
for (int time = 1; time < values[2]; ++time)
{
double A = values[1] * Math.pow(1 + values[3], time);
System.out.println("You have: " + A + " subscribers today");
}
}
public static double[] input()
{
Scanner s = new Scanner(System.in);
double[] values = new double[3];
System.out.println("Enter the Principal value: ");
values[0] = s.nextDouble();
System.out.println("Enter the no. of days: ");
values[1] = s.nextInt();
System.out.println("Enter the Rate of growth : ");
values[2] = s.nextDouble();
return values;
}
Try double[] values = input(); in main method.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class test { public static void main(String args[])
{
double[] values = input();
for(int time=1;time<values[2];++time){
double A=values[0]*Math.pow(1+values[2], time);
System.out.println("You have: "+A+ " subscribers today");
}}
public static double[] input()
{
Scanner s=new Scanner(System.in);
double[] values=new double[3];
System.out.println("Enter the Principal value: ");
values[0]=s.nextDouble();
System.out.println("Enter the no. of days: ");
values[1]=s.nextInt();
System.out.println("Enter the Rate of growth : ");
values[2]=s.nextDouble();
return values;
}
}
Again it is going to throw an arrayoutofbondsexception...change values[1] with values[0] values [2] with values[1] and values[3] with values[2]..
Related
I am a fairly new programmer in Java and am currently learning about how to incorporate multiple methods in one code. The goal of this practice activity is to use several different methods to:
-Create two arrays (one for employee names and another for how much that employee sold)
-Find the average of total sales
-Find the highest sale number
-Find the name of the Employee(s) with the highest sale count (and print "hooray" for every employee that had the highest sale count)
import java.util.*;
public class MethodActivity{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String[] names={"Employee A", "Employee B", "Employee C", "Employee D", "Employee E", "Employee F", "Employee G", "Employee H", "Employee I", "Employee J"};
System.out.print("Enter the sales numbers, in dollars, for each employee: ");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
int num4 = sc.nextInt();
int num5 = sc.nextInt();
int num6 = sc.nextInt();
int num7 = sc.nextInt();
int num8 = sc.nextInt();
int num9 = sc.nextInt();
int num10 = sc.nextInt();
double[] sales={num1, num2, num3, num4, num5, num6, num7, num8, num9, num10};
return double[] sales;
return String[] names;
}
public static double getAverage(double[] sales){
double average=(num1+num2+num3+num4+num5+num6+num7+num8+num9+num10)/10;
return average;
}
public static int getHighestSale(double[] sales){
double highest = sales[0];
int locationOfHighest=0;
if(sales[1]>highest){
highest=sales[1];
locationOfHighest=1;
}else if(sales[2]>highest){
highest=sales[2];
locationOfHighest=2;
}else if(sales[3]>highest){
highest=sales[3];
locationOfHighest=3;
}else if(sales[4]>highest){
highest=sales[4];
locationOfHighest=4;
}else if(sales[5]>highest){
highest=sales[5];
locationOfHighest=5;
}else if(sales[6]>highest){
highest=sales[6];
locationOfHighest=6;
}else if(sales[7]>highest){
highest=sales[7];
locationOfHighest=7;
}else if(sales[8]>highest){
highest=sales[8];
locationOfHighest=8;
}else{
highest=sales[9];
locationOfHighest=9;
}
return highest;
}
public static String showName(String[] names){
String nameOfHighest = "";
String hooray = "";
for (int i = 0; i<names.length; i++){
if (i=locationOfHighest){
nameOfHighest=nameOfHighest+names[i]+", ";
hooray = ""+"hooray ";
}else{
nameOfHighest=nameOfHighest;
}
}
return nameOfHighest;
}
public static void (String[] args){
System.out.println("The average sales for today was: "+average);
System.out.println(nameOfHighest+" made the highest sales of "+highest);
System.out.println(hooray);
}
}
However, when I run the program, I got these errors. The thing is, I don't really understand what they mean:
MethodActivity.java:20: error: '.class' expected
return double[] sales;
^
MethodActivity.java:21: error: '.class' expected
return String[] names;
^
MethodActivity.java:75: error: <identifier> expected
public static void (String[] args){
I would really appreciate if someone could clarify what these mean, since I'm still quite confused by the concept of a multi method code. And, if you could, maybe point out any other issues or fixable elements in my code (since I know my code might look pretty sloppy to someone with programming experience and I could really use some pointers). Thank you for your time.
You need to remove this both returns.
There are two problem in your code:
1) java method can have only one return statement.
2) it is main method and because of it returns void type. void means no return type.
Rather using separate method for printing "public static void (String[] args), print those in main method itself.
Also refer answer by iMBMT.
import java.util.Scanner;
public class MethodActivity {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number of employees : ");
int totalEmployeeCount = sc.nextInt();
System.out.println("## totalEmployeeCount : " + totalEmployeeCount);
String[] employeeNames = new String[totalEmployeeCount];
int[] employeeSoldCount = new int[totalEmployeeCount];
String name;
int count;
for (int index = 0; index < totalEmployeeCount; index++) {
System.out.print("Enter employee name : ");
name = sc.next();
System.out.print("Enter employee sale count : ");
count = sc.nextInt();
employeeNames[index] = name;
employeeSoldCount[index] = count;
}
System.out.println("---------------- Pringting all info ----------------");
for (int i = 0; i < employeeNames.length; i++) {
System.out.println("name : " + employeeNames[i] + " & sale count : " + employeeSoldCount[i]);
}
findTheAverageOfTotalSales(employeeSoldCount);
findTheHighestSaleNumber(employeeSoldCount);
}
private static void findTheAverageOfTotalSales(int[] employeeSoldCount) {
for (int saleCount : employeeSoldCount) {
System.out.println("Write your own code ...");
}
}
private static void findTheHighestSaleNumber(int[] employeeSoldCount) {
for (int saleCount : employeeSoldCount) {
System.out.println("Write your own code ...");
}
}
}
I am new in this field.
package test;
import java.util.Scanner;
public class Subject {
public static void main(String [] args){
System.out.println("Please Enter Subject No");
subNo =scan.nextInt();
System.out.println("Subject No Is : " + subNo);
for (int i = 0; i < subNo; i++) {
System.out.println("Please Enter Subject Name " + (i + 1));
subName[i] = scan.next();
}
}
public static int subNo;
public static String[] subName = new String [subNo] ;
static Scanner scan = new Scanner (System.in);
}
You have to initialize your array after you read subNo :
public static String[] subName;//<---------------not initialize it here
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please Enter Subject No");
subNo = scan.nextInt();
subName = new String[subNo];//<------------------initialize it here
System.out.println("Subject No Is : " + subNo);
Because when you run your program your array will be inisialize with the static subNo and not with the new one in your main
You must define the subName variable after of subNo variable, because this will be assigned after entering a value, for example:
public static int subNo;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please Enter Subject No");
subNo = scan.nextInt();
System.out.println("Subject No Is : " + subNo);
String[] subName = new String[subNo];
for (int i = 0; i < subNo; i++) {
System.out.println("Please Enter Subject Name " + (i + 1));
subName[i] = scan.next();
}
}
My current code is as follows, I need to print the user's input using the outputWithoutWhitespace() method but I am having a hard time understanding where to apply it/how to actually use it. I believe I should be using \t. How can I take away the whitespace when printing with the outputWithoutWhitespace() method?
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
int userCount = userInput.length();
return userCount;
}
}
package whitesp;
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
String omitSpace=userInput.replace(" ","");
int userCount = omitSpace.length();
return userCount;
}
}
Implement a method that is passed by two double type numbers, prints out their total and doesn't return anything. Guys how does this look now???
import java.util.Scanner;
public class Tutorial1 {
public static void main1(double num1, double num2)
{
double numb1=3.5;
double numb2=3.4;
double total = numb1 + numb2;
Scanner reader = new Scanner (System.in);
System.out.println(total);
numb1 = reader.nextInt();
numb2 = reader.nextInt();
}
I didn't understand what u want well try this hope helps to u
import java.util.Scanner;
public class MyFirstJavaProgram {
public static void main(String []args) {
main1(3.65,2.85);
}
public static double main1 (double numb12, double numb21){
int numb1 = 0, numb2 = 0;
int total = numb1 + numb2;
Scanner reader = new Scanner(System.in);
System.out.println("Enter two integers: ");
numb1 = reader.nextInt();
numb2 = reader.nextInt();
System.out.println(total+numb1+numb2);
return total+numb1+numb2;
}
}
I have an assignment to break an integer into it's individual digits, report them back to the user, and add them. I can do that, but I'm struggling with supporting negative integers. Here's my code, which works exactly the way I want it to, but only for positive integers:
import java.util.*;
public class Module4e
{
static Scanner console=new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Enter an integer: ");
String myNum=console.nextLine(); //Collects the number as a string
int[] asNumber=new int[myNum.length()];
String []upNum=new String[myNum.length()]; //updated
int sum=0; //sum starts at 0
System.out.println("\n");
System.out.print("The digits of the number are: ");
for (int i=0;i<myNum.length();i++)
{
upNum[i]=myNum.substring(i,i+1);
System.out.print(upNum[i]);
System.out.print(" ");
sum=sum+Integer.parseInt(upNum[i]);
}
System.out.println("\n");
System.out.print("The sum of the digits is: ");
System.out.println(sum);
}
}
I've found plenty of hints for getting this to work with positive integers, but none for negatives.
Use RegExp
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestDigits {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Validate Input
String number = console.nextLine();
Pattern p = Pattern.compile("(-?[0-9]{1})+");
Matcher m = p.matcher(number);
if (!m.matches()) {
throw new IllegalArgumentException("Invalid Numbers");
}
// Calculate
p = Pattern.compile("-?[0-9]{1}+");
m = p.matcher(number);
int result = 0;
System.out.print("The digits of the number are: ");
while (m.find()) {
System.out.print(m.group() + " ");
result += Integer.valueOf(m.group());
}
System.out.println("");
System.out.println("Result " + result);
}
}
// I think you can use this code //also you can multiply the number by -1
int positive = 0;
//positive give you information about the number introduced by the user
if (myNum.charAt(0)=='-'){
positive=1;
}else{
positive=0;
for (int i=positive; i<myNum.length(); i++){
//be carefull with index out of bound exception
if ((i+1)<myNum.length()){
upNum[i]=myNum.substring(i,i+1);
}
}
Change the statement String myNum=console.nextLine() to String myNum = String.valueOf(Math.abs(Integer.valueOf(console.nextLine())));
You do not have to use String to solve this problem. Here's my thought.
import java.util.*;
public class Module4e throws IllegalArgumentException {
static Scanner console=new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter an integer: ");
if (!console.hasNextInt()) throw new IllegalArgumentException();
int myNum=console.nextInt();
myNum = Math.abs(myNum);
int sum=0;
System.out.println("\n");
System.out.print("The digits of the number are: ");
While (myNum > 10) {
System.out.print(myNum % 10);
System.out.print(" ");
sum += myNum % 10;
myNum /= 10;
}
System.out.println(myNum);
System.out.print("The sum of the digits is: ");
System.out.println(sum);
}
}
Try this. I gave -51 as input and got -6 as output. This is what you are looking for?
import java.util.*;
public class LoggingApp
{
static Scanner console=new Scanner(System.in);
public static void main(String[] args)
{
int multiple = 1;
System.out.print("Enter an integer: ");
String myNum=console.nextLine(); //Collects the number as a string
Integer myNumInt = Integer.parseInt(myNum);
if (myNumInt < 1){
multiple = -1;
myNum = Integer.toString(myNumInt*-1);
}
int[] asNumber=new int[myNum.length()];
String []upNum=new String[myNum.length()]; //updated
int sum=0; //sum starts at 0
System.out.println("\n");
System.out.print("The digits of the number are: ");
for (int i=0;i<myNum.length();i++)
{
upNum[i]=myNum.substring(i,i+1);
System.out.print(upNum[i]);
System.out.print(" ");
sum=sum+Integer.parseInt(upNum[i])*multiple;
}
System.out.println("\n");
System.out.print("The sum of the digits is: ");
System.out.println(sum);
}
}