Recursion - Returning Array Of Odd Numbers

Feb 26, 2014

I have this method

Java Code:
public static int[] oddNums(int n){
} mh_sh_highlight_all('java');

When a number is passed in, this method returns an array of odd numbers the amount of the number passed in.

So

Ex: Class.oddNums(5) would return [ 1, 3, 5, 7, 9 ]
Class.oddNums(2) would return [ 1, 3]

View Replies


ADVERTISEMENT

Recursion - Converting Numbers To Strings

Jul 7, 2014

I found an open-source recipe for converting numbers to words.

I modified it a bit since the requirement is only for integers until 999:

public class NumberToWordsConverter {
private String[] ones = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};

[Code] .....

From robosoul's response at StackOverflow, I simply inserted a condition for negative integers (line 12). True enough, the code worked and I was able to trace (pen and paper method) how it is converting the 0 and positive integers to words.

However, I am a bit lost on how it is doing the conversion for negative integers. How it is successfully converting the negative?

View Replies View Related

Sum Of Array By Recursion

Mar 25, 2015

How to add the sum of an array with a recursion, but I don't understand how to use recursion. I just understand that it calls back the method. I am nearly done with the code.

import java.util.Scanner;
class Question1{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
int size, sum;
System.out.println("Please input how many numbers will be used");
size=s.nextInt();

[Code] .....

View Replies View Related

Array Recursion Sum Is Not Producing Correct Output?

Feb 2, 2015

I am trying to sum up the elements of an array. When I test my code the sum is always off by one. For example if I input: 20, 40,30 it gives me 89 instead of 90.

This is what I have so far:

public static void main(String[args]){
int size = kbd.nextInt();
int [] myArray = new int [size]
//user inputs the elements of array
for(int i =0; i<myArray.length; i++){
myArray[i]= kbd.nextInt();
}
int total = sumNum(myArray,0, myArray.length-1)
System.out.println("The sum is"+ total);
}

[code]....

View Replies View Related

Recursion Method With 2d Array To Find A Column With Lowest Sum

Jan 28, 2015

i have to write a method, The method receives a parameter of two-dimensional array of integers. The method returns the number of the column which has the lowest sum of the integers.I'm allowed to use only recursion! no loops allowed!-of course i need to make a private method that will sum a column as a single array and then i have to do another private method that compares the column , but it doesn't really work .

View Replies View Related

Create A Recursion Method That Takes ONLY ARRAY As A Parameter

Mar 9, 2015

so i have this question where it wants me to create a recursion method that takes ONLY THE ARRAY as a parameter, and without using loops or static variables inside the method, and then the method returns the smallest value in that array. However, i tried making the simple if statements where i compare the first element of the array with the second element using the length of the array and decreasing it to get the next elements and compare it again by calling the recursion method, but the problem is when i call the method again, the length does not decrease, even if i store it in a variable, the variable will initialize itself again, and the length wont change.

View Replies View Related

Java Recursion Method Is Not Working - Summing Elements In Array

Feb 2, 2015

I am trying to sum up the elements of an array. When I test my code the sum is always off by one. For example if I input: 20, 40,30 it gives me 89 instead of 90.

This is what I have so far:

public static void main(String[args]){
int size = kbd.nextInt();
int [] myArray = new int [size]
//user inputs the elements of array

[Code]....

View Replies View Related

Returning Int Array Using Generics

Apr 10, 2014

public int[] allIndicesOf(E itemSought) {
ArrayList<Integer> toUse = new ArrayList<>();
for (E anArray : container) {
if (anArray.equals(itemSought)) {
toUse.add(container.indexOf(itemSought));

[Code] ....

I have an array list of strings. I want to be able to return an array of integers telling me which indexes in the string array list contain the itemSought object.

View Replies View Related

Method Is Not Returning Array - Null Error

May 18, 2015

I am trying to return an array and I keep getting a null error. The below class sets the material numbers into an array and should return that array if called :

public class Jobs {
private int[] materialsNumber;
//change to parts and create another class that gets the materials for the parts
 public int[] job1() {
materialsNumber[0] = 11960120;

[Code] ....

I later try to call the method. The program executes but stops after I println "test in loop"

public class PurchaseOrdersToParts {
 private Jobs job = new Jobs();
 int[] getPartsForPurchaseOrder(BigDecimal purchaseOrder) {
 System.out.println("inside getparts");
 BigDecimal testNum = new BigDecimal(123.0);
 
[Code] ....

This is the method that is calling the method in the GenerateOrdersToParts class

private PurchaseOrdersToParts purchaseOrdersToParts = new PurchaseOrdersToParts();
 @Inject
PoRepository poRepository;
public GenerateShopJobTickets() {
 
[Code] .....

View Replies View Related

Returning Index Of Last Occurrence Of A Number In Array

Apr 16, 2014

I'm making a program with several functions for an array of integers, one being to find the last occurrence of a given integer and returning it. The problem is that the method is returning -1 for every input, even though it's only supposed to do so for numbers not in the array. Here's the part of my code with the sections I'm having an issue with:

Driver

import java.util.Arrays;
public class ArrayMethodsDriver
{
public static void main(String[] args) {
int[] a = {7,8,8,3,4,9,8,7};
System.out.println("Last index of 8: " + ArrayMethods.findLast(a, 8));
System.out.println("Last index of 2: " + ArrayMethods.findLast(a, 2));

[Code] ....

As you can see, I'm testing 8(which should give a 6) and 2(which should give -1 because it's not in the array), and 4(which should give 4). The print statements all say -1.

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.

[ public class Array1
{
public static void main(String[] args)
{
int array [ ] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 26);

[Code] .....

View Replies View Related

Merging And Sorting Array Lists Code Is Returning 0s?

Apr 19, 2015

I'm working on an assignment that asks for the user to input 2 lists of numbers and my program will merge and sort the lists using arrays and 2 methods. I think I have most of it down, but I'm not sure how to go about getting the user inputs. In my current code, it's giving me a bunch of 0s instead of a sorted list.

import java.util.Scanner;
public class merge2sortedlists {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Resize Array And Add Numbers

Oct 21, 2014

I need to make a code that will ask the user for an array size and the numbers that go in the array. Then it must ask for a new size and copy the numbers from the first array and add numbers to fill the new array.

import java.util.Scanner;
import java.util.Arrays;
public class Lab07{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");

[Code] ....

I can make the first array but I am getting stuck on the second this is what I am getting back

How many numbers do you want to enter?
3

Enter the 3.0 numbers now.
7
1.2
9

These are the numbers you have entered.
[7.0, 1.2, 9.0]

How many numbers do you want to enter?
5

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Lab07.resizeArray(Lab07.java:27)
at Lab07.main(Lab07.java:18)

how do I get the new array and not the error

View Replies View Related

Storing Set Of Numbers In Array?

Oct 9, 2014

I trying to write a piece of code which takes a set of numbers or even data type and stores in an array (or something more suitable).

For example:

Date N1 N2 N3 N4 N5 Day
10/11/2012 10 09 32 100 15 Thursday
11/12/2013 01 0 0 23 50 51 Tuesday

I'd like to be able to sort them so that if I want to search for one of the entries, I can then create a function which allows me to sort them by date or even return all the numbers by a given date or day, etc....

However, I'd like to be able to set it up so that each set is "linked" meaning that again that I can search by date and it returns everything at that date.

I wanted to use an array but I don't know:

-How to do it?

-Whether this is a suitable approach?

View Replies View Related

How To Add Numbers In A Column In Multidimensional Array

Mar 13, 2015

import java.util.Scanner;
public class ColumnSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;
int [][] matrix = {{5, 9, 87, 74, 12, 7}, // row 1

[code]...

Right now my code gets all the numbers in a row and adds those up, but I want it to get the numbers in a column and add them instead. The problem is I don't know how to get the userpick (the number that the user picks to determine which column gets added) to be set to that particular column.

View Replies View Related

How To Print All Numbers Of Array In Java

Jan 1, 2015

This is my code

package com.arraydemo;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class ArrayStructures {
public long[] theArray;
public int arraySize;
public ArrayStructures(int size)

[code]....

and i am getting this output

----------
! 1712 !2156|
----------
! 1713 !4583|
----------
! 1714 !3981|
----------

[HENRY: 6000+ LINES DELETED -- Isn't it a bit ridiculous hard to read when you flood a post with thousands of output lines?]

----------
! 4998 !3094|
----------
! 4999 !836|
----------
12:10:56

I am expecting Number to be print from Index ) but they are not printing ,why?

If I use arraysize like 2000 I am getting all number starting from 0 to 1999 .why?

View Replies View Related

How To Get Sum Of All Numbers In A Row Of 2D Array With Row Being Selected By User

Mar 9, 2015

I just learned about 2D arrays and am still trying to get a grasp on the concepts. I'm a little confused by how you return all the values in a row to add up and display the sum if the row is entered by the user.

import java.util.*;
public class RowSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;

[Code] .....

View Replies View Related

Program That Use Array To Store 10 Numbers

Oct 16, 2014

i'm trying to write a program that uses an array to store 10 numbers. The numbers should be randomly generated ( Math.random() ), and they should be between 1 and 100 ( 1 and 100 inclusive ). The program should produce an output like the one below:

Element 1 = 23 ( Odd )
Element 2 = 15 ( Odd )
Element 3 = 32 ( Even )
Element 4 - 10 ( Even )
Element 5 - 99 ( Odd )
Element 6 - 1 ( Odd )

[Code]...

I have written code for this but its only showing me 0's after first number can you check whats wrong with my code. my code is.

class even/odd{
public static void main(String[]args){
int y=0;
int z=0;
int[] array= new int[11];
for(int x=1; x <array.length ; x++){
array[x]= (int) (Math.random()* 100);
 
[Code]...

View Replies View Related

Using Do While Loop With Array For Phone Numbers

Apr 20, 2014

I need a phonebook that allows the user to input up to 100 numbers but stop when they want to. I have the code for the array and input and output. I thought I had the do...while loop correct for asking the user if they want to input another name but its not working, even when you input "N" it still keeps going. Here's my code.

import java.util.Scanner;
import java.util.ArrayList;
public class PhoneEntry2 {
int x;
char repeat;
final int maxContacts = 100;
String[] pNums = new String[100];

[Code] ......

View Replies View Related

How Many Numbers In Array Have Value Greater Than Or Equal To 100

Sep 10, 2014

I have an assignment that wants me to write a Java function based on induction to determine how many numbers in an array have a value greater than, or equal to, 100.

I have started with:

Java Code:

int recurseHundred (int [] A, int n) {
//n is the number of elements in the array.
//Base case:
if (n == 1 && n >= 100) return A[0];
//Recurse
int num = recurseHundred(A, n-1);
if (n-1 >= 100) return A[n-1];
else return num;
} mh_sh_highlight_all('java');

I don't think this actually does the trick.

View Replies View Related

How To Shuffle Array Using Random Numbers

Nov 10, 2014

For a project we have to "shuffle" items in an array using random numbers. We are supposed to generate random numbers and use those numbers to exchange array elements. But I am not sure what that means, "exchange array elements". Does that mean you generate 2 random numbers within the length of the array, and then switch the items at those locations in the array?

View Replies View Related

Array Of Numbers Not Able To Get Correct Average Value

May 26, 2015

I have the following methods:

public static int getSum(int[] data) {
int sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i];
}
return sum;

[code]....

The input is the following arary (Its from the Junit test that fails this): [Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE].I get an average of 0.0 when it should be -0.2.

View Replies View Related

Placing Numbers Inside Array

Apr 3, 2015

"on each step of the loop you can calculate an entire year as opposed to a single month, otherwise you can calculate monthly, but instead of printing, create an array and fill it up with your solutions, then have a second loop where you add them up by dozens to print yearly values, alternatively, have a counter k that adds up to 12, and two variables for adding up the monthly results, increment these after you calculate the monthly value, and whenever k hits 12, print them and reset to zero. print a final time once you leave the loop. "

int year = loanDurationYears-14;
int[] years= new int[loanDurationYears];
periods = loanDurationYears*12;
for(int i=0;i<1;i++)
{
for(year = 0;year<loanDurationYears;year++)
{
years[year] = year+1;
 
 [code]....

View Replies View Related

Store Numbers In Array Then Sort

Feb 19, 2014

Code below. I am not sure if my logic is correct. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.

import java.util.Scanner;
class regnumber
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("registration number ", 100,1000);

[Code] ....

View Replies View Related

Random Array - Only 2 Of 10 Possible Numbers Are Generated

Nov 21, 2014

I am writing a program that creates an array with random numbers. Then the user can choose what number of the array he/she wants to check the occurance of. This works fine, but the numbers generated seems very weird, only 2 of the 10 possible numbers are generated. 0 and one of the other 9 numbers, the 0 is always 10100 and the other one is always 101.

import java.util.Random;
import java.util.Scanner;
public class OppgaveC {
public static void sjekk() {
int randomArray[]=new int[101];
int countArray[]=new int[10];
Random rand = new Random();

[Code] ....

it also says i have a memory leak on my scanner, ow can i close this?

View Replies View Related

Find Arithmetic Mean Of The Numbers Stored In Array

Feb 23, 2014

The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.

Here is what I have so far:

double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 };
ArrayList<Double> d = new ArrayList<Double>();
ArrayList<Double> e = new ArrayList<Double>();
for ( double x : q ) {
d.add( x );
e.add ( new Double ( x ) );

[Code] .....

Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved