Multiply Two Numbers - Add X Number Y Amount Of Times
Jun 9, 2014
I am trying to make a program to multiplies two numbers without using the "*" operator. My idea was to add x number y amount of times, which is the same as multiplication. I did this with a for loop, but zero is always returned as the answer. How can I fix this?
public class secondMult {
public static void main(String[] args){
System.out.println(multWithSum(4,5));
}
public static int multWithSum(int x, int y){
int sum = 0;
for(int i = 0; i==y;i++){
sum = sum + x;
}
return sum;
}
}
View Replies
ADVERTISEMENT
Jun 14, 2014
I am having is not being able to print out the diamond the amount of times the user enters (for example, when it asks how many diamonds, I enter a 2 and only one diamond comes out.)
import java.util.Scanner;
public class Pattern5
{
public static void main(String[] args)
{
// For this pattern program, ask the user how many diamonds
// she would like to print to the screen. The following pattern is one diamond.
[code]...
View Replies
View Related
Oct 27, 2014
Ii am trying run a do while loop on java a specific amount of times for example if i were to type in "Enter number of years" and enter 4. i need the loop to execute 4 times or if i said 6 times then i would be 6. the program i have repeats it and takes it back to reentering "Enter number of years" i need it to execute by its self x amount of times. here is what i have so far
System.out.println("Enter the number of years: ");
years = keyboard.nextInt();
rainfall = generator.nextInt(10) + 1;
System.out.println("Year " + "rainfall amounts were");;
for (int months = 1; months <= 12; months++)
[Code]...
View Replies
View Related
Nov 2, 2014
My assignment is to write some code that will multiply every number in an array by 2 and print it out. This is using a site zyante which is a interactive online book kind of thing.
I have tried For (i=0; I < 8; i++) with like userVals = userVals * 2) }
And it doesn't like that so i'm guessing i am no where close to right. The chapter doesn't give me any example of doing anything close to this so i am completely lost on what i have to do.
This is the program :
import java.util.Scanner;
public class NegativeToZero {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_ELEMENTS = 8; // Number of elements
[Code] .....
View Replies
View Related
May 12, 2014
public static void main (String [] args) {
int a = (int) 0.25;
int b = (int) 1.25;
int result = a + b;
System.out.println("Result of addition is "+ result);
}
}
View Replies
View Related
Jul 4, 2014
I am trying to make a program that get input value 1-200 from user. However I would like to make the program to show warning if the input number is not within the range of 1-200. In the end I would like to show the amount of number according to its interval (i.e less 50, 51-100 and so on) ....
import java.util.Scanner;
class test {
public static void main (String[] args) {
int number[]= new int[5];
int number2[]= new int[5];
int a=0,b=0,c=0,d=0,i=0;
[Code] .....
View Replies
View Related
Apr 7, 2014
I'm doing this project in JSP.net, with html. and in one of the web pages, what I'm doing is that, a person is transferring an amount to someone else, and if the transfer amount is more than the account balance, then he cannot transfer. else, save details in a table.
Here's the code I've used
<%
String t1=request.getParameter("text1");
String t2=request.getParameter("text2");
java.util.Date date1 = new java.util.Date();
SimpleDateFormat ft = new SimpleDateFormat ("dd/MM/yyyy");
int tamt = Integer.parseInt(t2);
int bamt = 0;
[Code] .....
But when i enter the fields and click the submit button in the form, to save it in the table, i get the error:
"org.apache.jasper.JasperException: java.lang.NumberFormatException: null"
(I've attached a screen shot of the error) : NumberFormatException.bmp (750.98K)
What does this mean? how do i find the line in the code that has the error?
View Replies
View Related
Oct 3, 2014
I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...
This is the question:
Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:
printStrings("abc", 5);
will print the following output: abcabcabcabcabc
This is what I attempted:
public class printStringsproject {
public static void printStrings(int abc, int number) {
for (int i = 1; i <= number; i++) {
System.out.print("abc");
}
}
public static void main(String[] args) {
printStrings(1, 5);
}
}
View Replies
View Related
Mar 22, 2014
I read on how to use the continue statement, but I'm failing in how to use it properly, mostly because it's not working. I'm supposed to print out what numbers are showing up and how many times for each. Plus, I have to print out 'times' instead of 'time' if there's more than one of a certain number. Right now, it's printing out all the numbers including the ones that don't get inputted.
import java.util.Scanner;
public class occurrence {
public static void main(String[] args) {
//scanner/values
Scanner input = new Scanner(System.in);
int number = 101;
[Code] ....
View Replies
View Related
Oct 26, 2011
I'm trying to write a method that will print a string of a random number of X's (from 5 to 20) on a line, then another number of X's on the next line, etc. until the random number = 16.
I have to use while loops and probably fencepost algorithms. I'm confused with how to print the number of x's the random number of times over and over.
Right now, I'm pretty sure my for loop is wrong or unnecessary.
So far, this is my code:
public static void randomX() {
int x = 0;
while (x != 16) {
x = (int)(Math.random()*20) + 5;
for (int i = 0; i <= x; i++);
{
System.out.print("x");
}
}
}
View Replies
View Related
Dec 7, 2014
This is my code up to now and I can't do anything to make it work. I want it to tell me how many times the number 3 appears, and the last position it was in. I am getting errors like"Cuanto.java:88: getPosition(double[],double) in Cuanto cannot be applied to (double) ....
a = getPosition(a);" and unreachable statements, and value not found. It's all over the place every time I make a change to it.
Java Code:
public class Cuanto {
static int getPosition(int count,double listOfValues[],
double targetValue) {
int a = 0, i;
for (i=0; i < listOfValues.length; i++) {
if (listOfValues[i] == targetValue) {
a++;
[Code] ....
View Replies
View Related
Jan 4, 2015
How do I record the number of times all runs of heads occur. Ex.
Length Number of runs of heads
1 --------------------3
2 --------------------2
3 --------------------0
4 --------------------1
public static void main(String[] args)
{
int [] coinToss = new int[TOSSES];
int longestRun = 0;
int run = 0;
for(int i = 0; i < coinToss.length; i++)
coinToss[i] = toss();
[Code] .....
View Replies
View Related
Nov 16, 2014
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) {
int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 };
int num = 3;
int count = numbers(a, num);
for (int i = 0; i < a.length; i++) {
[Code]...
View Replies
View Related
Mar 7, 2014
Write a program using a while-loop (and a for-loop) that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. (So, you will have two separate program codes, one using a while-loop and the other one using a for-loop.)
Example:
Enter a string: "Hello, JAVA is my favorite programming language."
Enter a character: e
The number of times the specified character appears in the string: 3
I don't even know where to begin I've only got this
import java.util.Scanner;
public class letterCounter {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String myString = sc.nextLine();
System.out.println("Enter a letter");
String letter = sc.nextLine();
}
}
View Replies
View Related
Mar 26, 2014
I am trying to flip a coin 1000 times and make the driver class count the number of times a head and tails appear. However, I am having some trouble trying to figure out the error:
package Week5;
import java.util.Random;
public class Coin {
private int heads;
private int tails;
private int face;
[Code] ....
I was able to print out the generated numbers, but java will print either heads or tails a thousand times and count that as 1000 and nil for the other probability...
View Replies
View Related
Nov 20, 2014
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
If the list is empty, return 0.
View Replies
View Related
Sep 7, 2014
I need to write a simple program that reads an amount of minutes and displays the approximate number of years and days for the minutes. For simplicity, assume a year has 365 days and the resulting amount of days is a whole number.
View Replies
View Related
Mar 28, 2014
I cannot get the right output for my program. I need to get the total number of pennies in dollar amount for the output of Total Pay. This is my program. The directions are:
Write a program that calculates the amount a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Do not accept a number less than 1 for the number of days worked.
import java.util.Scanner;
import java.text.DecimalFormat;
public class PenniesForPay {
public static void main(String[] args) {
int numDays;
[Code] ....
totalSalary should be total number of pennies / 100....However its not picking up only day 30 of pennies which is 536,870,912 pennies and then dividing it?
View Replies
View Related
Mar 12, 2015
Develop a method that accepts as data input three integer numbers, calculates the sum and the product of the numbers, and displays the sum and product.
View Replies
View Related
Apr 14, 2014
I would like to multiply the amount stated inside program by user input. I currently have
public static void setTicket(double ticket)
{
//if statement for tickets
if (ticket == 1)
Ticket = 10.00;
else if (ticket == 2)
Ticket = 20.00;
else if ( ticket == 3)
Ticket = 30.00;
The user should enter 2 and the value should show 20.00. This does work however im looking for a way to say enter 2 = 2*10 instead of stating each value individually.
View Replies
View Related
Mar 19, 2014
I was suppose to create a simple Java program for calculating the area of a rectangle (height * width). Then check the user’s input, and make sure that they enter in a positive integer, and letting them try again if they enter in a negative number. (I'm not sure how to get them to try again.
I am suppose to use an "if" statements and indeterminate loops to achieve the solution. The program will have the following requirements:
1. Ask the user to enter in both a height and width (as an integer)
2. If the user enters in a negative number, display an error
3. If the user enters in a negative number, give them another chance to enter in the correct value
4. Calculate the area and display to the screen once two positive integers have been entered.
import java.util.Scanner;
public class RectangleAreaCalc
{
public static void main(String[] args)
{
int length;
int width;
int area;
[Code] ....
View Replies
View Related
Sep 21, 2014
I have a program where the user enters in the miles and then it is supposed to get multiplied by feet but I keep getting this error.
GetUserInfo.java:12: error: bad operand types for binary operator '*'
int total = name * feet;
^
first type: String
second type: int
1 error
This is my code so far :
import java.util.Scanner;
public class GetUserInfo
{
public static void main(String[] args) {
String name;
int miles;
[Code] ....
View Replies
View Related
Mar 27, 2014
Where I am doing mistake to print the numbers of Armstromg numbers requested.
import java.util.Scanner;
public class ArmStrongNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x, y, z=0, temp, temp1=1;
[Code] .....
View Replies
View Related
Feb 13, 2015
The assignment is to make a program that prints the number of prime numbers in a range. This is what i have so far. The output is a list of 2s. I created the for loop to cycle through the range of 17-53 and nested a while loop within to test each incident of the for loop to check for divisors starting with 2 until the modulus result is 0 resulting in a false for being a prime number. Then the loop should increment to the next i value. The last part is an if statement that i had intended to add counters to the k variable that would keep track of the number of prime numbers.
boolean isPrime = true;
int j = 2;
int k = 1;
for (int i = 17; i <= 53; i++){
{
while (i % j == 0){
isPrime = false;
[Code] .....
View Replies
View Related
Oct 31, 2014
I found an exercise online to create a small program . I have this code that I have done so far:
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong(); long b = sc.nextLong();
long count = 0; // counter
for (long c = a; c <= b; c++) {
if (c % 2 == 0 || c % 3 == 0 || c % 5 == 0) {
count++;
}
}
System.out.println(count);
}
}
This program is suppose to give me the number of numbers which are dividable by either 2,3 or 5 in a range of a to b, where a<=b.
FOR EXAMPLE: a=5 b=8 ... output: 14 (since there are 14 numbers in between 5 and 8 which are dividable by either 2,3 or 5.)
It works great for all of the numbers except higher ones such as a=123456789012345678 b=876543210987654321. Here it doesn't give me any output. From what I know it is because the code is still running. But there must be a quicker way ...something that can modify the code so it finishes in the matter of seconds not hours. Something that will fasten the process of checking if the numbers are dividable...
View Replies
View Related
Jul 29, 2014
I want to be able to
(A) Restrict the number of output values per line to 3 numbers,
(B) Remove Duplicate lines, where the same numbers are duplicated just outputted in a different order.
(C) Count the number of outlines
(D) Add a fixed column to the outputs that has an ascending count from 1 upwards
I'm trying to achieve. The code is as follows:
package num.com.t1;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
*
*/
public class NumComT1 {
public static void main(String... args) {
[Code] ....
View Replies
View Related