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


ADVERTISEMENT

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 View Related

Converting Strings To XML And Back To String

Mar 3, 2014

I'm stuck with the following error message when i try to convert a String to XML and back to a String.
 
Exception in thread "main" org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
    at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(Unknown Source)
    at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(Unknown Source)
    at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(Unknown Source)
    at com.innovatrail.consulting.xml.XMLTEST2.main(XMLTEST2.java:66)
 
Below is  my Java code snippet: The code syntactically correct except line 66. If you comment out line 66 in the source code, everything works just fine. The XML representation of my input string is denoted by INPUT 1 and that of my output string is denoted by OUTPUT 2.
 
package com.Test.xml;
 
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;

[Code] ....
 
INPUT 1:

<Collection>
<BookList>
  <Book>
    <Title>Wonder Man
    </Title>
    <Author>John Smith
    </Author>
    <Category>Fiction

[Code] .....
 
OUTPUT 2:

<Collection>
  <Book>
    <Title>Wonder Man</Title>
    <Author>John Smith</Author>
    <Category>Fiction</Category>

[Code] ....

View Replies View Related

Converting Strings To Bits / Bytes And Vice Versa

Feb 23, 2014

I have:

Java Code: String s = "111100100111011011000010110011101"; mh_sh_highlight_all('java');

I am trying to convert that to bits/bytes.

I need to make that into a series of bits with the same exact values..."1" = a 1 bit, and "0" = a 0 bit.

Remarkably, I haven't been able to find much on this sort of conversion - possibly I am just not searching with the right keywords? As typically, stackexchange or other parts of the web have the same question that I have, asked (and answered) by many others.

How do I go about doing this?

Furthermore, how would I go about saving this to a file - and are there already good "kinds" of files to save this into. If not, how do I go about making my "own" type of "file."

CONTEXT:

I've written a compression system. To keep things simple, I've been using a string to hold the 1s and 0s, so that debugging is simpler, and overall, everything is easier to write. Now, however, my algorithm is finished - and I'm moving on to create a GUI and a working system. This is the last step that I need for the non-GUI stuff (which I'm writing through javafx by the way - is this the right thing to use? I've been told that that is where people are moving towards. Away from swing).

View Replies View Related

Complex Numbers - Converting Syntax?

Mar 17, 2015

Convert to following sysntax (from Matlab) having complex nos.

real((413000*atan((T^(1/2)*((4130000*V*1i)/(139*T^(1/2)) + (413000*1i)/T^(1/2)))/413000)*1i)/(139*T^(1/2)))

View Replies View Related

How To Concatenate Strings Of Binary Numbers Together

May 23, 2015

Ok so what I a String array of binary numbers

private String[] encodedNumbers = {"0000", "0001", "0010", "0011","0100", "0101", "0110","0111","1000","1001","1010","1011","1100","1101"};

and what I want to do is simply run through a loop (Which I already made ) that will randomly just put this strings together.
So if the loop runs once it will return something similar to

00001011001101000110

The thing is that I don't want to return such a number as an String, I want it to be returned as a long.This was one of my recent attempts

/* Contains the genes or possible solutions to the problem
*/
public class Genes
{
/* Each element is a binary number that corresponds to index number they have been assigned to, these are the possible genes
* The last 4 elements in the array represent + - * / correspondingly
*/
private String[] encodedNumbers = {"0000", "0001", "0010", "0011","0100", "0101", "0110","0111","1000","1001","1010","1011","1100","1101"};

[code]...

What can I do ? I want them the values returned to be of the long data type, but I don't want the binary numbers to be added together. I just simply want them to be placed one next to each other at random patterns

View Replies View Related

Accurately Converting Units Involving Extremely Small Numbers?

Jun 16, 2014

I need to convert units accurately. Some of these units include angstroms (0.1nm) and astronomical units or light years. I can't seem to accurately perform these equations (probably mostly because of lack of a proper conversion formula). For example:

1 angstrom = 1.79985601 × 10^-14 leagues (or, 0.00000000000001799856 leagues).

To perform the conversion right now, I'm simply multiplying the angstroms by 0.00000000000001799856. However, inputting 55560000000000 (angstroms in one league) results in 0.9999999936. Why is this? Is it possible to work around it? I believe there's a term for the inability for a computer to process 1/3rd properly, but I can't remember what it's called - is that the problem?

I am using BigDecimals for all equations.

Edit: a mod can change the title if they think another is more fitting.

View Replies View Related

Sorting A Text File With Strings And Numbers

Oct 16, 2014

how to sort my text file. So far I have been able to read the text file and print it back out, but I am unsure of how to go about sorting it. Must print the colors (in the order of the rainbow first) and if the colors are the same compare the size (bigger is more important)The values I have to sort are written as such in the text file:

blue 18
blue 10
red 27
yellow 4

public class Rainbow{
private String color;
private int size;
public Rainbow(String color, int size){
this.color = color;
this.size = size;

[code]....

I would know how to sort it if it was supposed to be alphabetical order or there were only numbers, but I can't seem to figure out how to sort it when there are strings and integers

View Replies View Related

Need To Count Strings That How Many Inputs From User Were Numbers

Jan 30, 2014

import java.util.Scanner;
public class CountStringNumbers { // show the number of Strings that were numbers . . ..
public static void main(String []args){
int count = 0;
int countNumbers=0;
Scanner input=new Scanner(System.in);

[code]...

can count the numbers from 0-9 if the user puted but if it is 10 it doesnt count it , and i need to write all the numbers to have that option. . . . Is there an easy way that includes all the numbers? like if(word.equals(number)){<<<???? (numbers) i know htat doesnt work but is there anything similar ?? ??

countNumbers++;
}
System.out.println("Type Something , , , and to end the the termination type STOP");
word=input.nextLine();
count++;
}
System.out.println("You wrote "+count+" lines wich from those lines "+countNumbers+" were numbers and the programe terminated ");
}
}

View Replies View Related

Place Leading Zeros Onto Numbers Without Having To Convert Them To Strings

Jan 21, 2014

Any way to place leading zeros onto numbers without having to convert them to strings? Is such a thing possible?

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings?

Feb 3, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method

import java.util.Scanner; 
public class Number{
private String whole;
private String decimal;
private String sign;
  public static void main (String[] args){
 System.out.println("Enter two numbers");

[code]....

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings

Feb 5, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction .

Java Code:

import java.util.Scanner;
public class Number{
private String whole;
private String decimal;
private String sign;

[Code] .....

View Replies View Related

Convert Strings To Double Numbers - Exception When Non-numeric Entered

Nov 11, 2014

So I have to convert strings to double numbers and there can be no exception.

The strings that aren't numbers or do not fit into a set criteria have to be discarded.

When I try to write this I get an exception when a non-numeric is entered and the code stops.

What can I do? Also, am I finding the average of the array correctly?

import java.util.*;
public class Grades{
public static void main(String args[]){
int arraycount = 0;
final int SIZE = 10;
int validArraycount = 0;
final int ValidArraySize = 10;

[Code] ......

View Replies View Related

How To Find Whether Strings Contained In Array Of Strings Constitute A Similar Character Or Not

Aug 25, 2014

I am having an array of strings and i want to find out whether these strings contained in the array contain a similar character or not.For example i am having following strings in the array of string:

aadafbd
dsfgdfbvc
sdfgyub
fhjgbjhjd

my program should provide following result: 3 because i have 3 characters which are similar in all the strings of the array(f,b,d).

View Replies View Related

Sorting Lowercase Strings Before Uppercase Strings

Mar 26, 2014

I can sort strings in a collection by uppercase and then lowercase though I was wondering if there is any way of doing it in reverse, sorting by lowercase then by uppercase.

View Replies View Related

Recursion On Initialization

Apr 29, 2014

I am trying to make a game, for some reason i have begun to get a java.lang.StackOverflowError.

I am not exactly sure how i can fix it. only removing line 14 from infopannel1 (and everything that used that class.) seems to work. im not sure what else i can do to fix it. or why its resulting in stack overflow for that matter.

I am putting in a link for the files i wrote this using bluej (several classes have no relevance, errorv2, demonstration, folderreadertest, ReadWithScanner, saveloadtest, menutest,rannum, and menutestTester. are all irrelivent to my problem.)

[URL] .....

View Replies View Related

How Does Recursion Work

Dec 13, 2014

how does recursion works, I don't understand why it prints al numbers going up again?This is the code

void print2(int n){
if (n<0){
out.printf(" %d",-1);
return;
}
out.printf(" %d", n);
print2(n-1)
out.printf(" %d", n);
}

this should be the output if n is 6: 6 5 4 3 2 1 0 -1 0 1 2 3 4 5 6.

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

Recursion And Lists?

Aug 6, 2014

public void myFunc(MyNode n, ArrayList<MyNode> path) {
boolean hasChildren = false;
path.add(n);
int index = path.indexOf(n);
ArrayList<MyNode> statefulPath = new ArrayList<MyNode>();

[Code] ....

I have similar code that I stepped through in a debugger. After running the code I found that it built the desired tree, in this case a root node H with left child L and right child P. I want list of lists to contain all paths from root to leaf. I expected [H, L] and [H, P]. I discovered that statefulPath is not stateful; after a recursive stack frame pops, statefulPath still contains n! But that stack frame just popped! I expected to see statefulPath be [H] in the debugger and it was [H, L]! So I later have a list [H,L,P] which I don't want. How do I make the statefulPath list I want for my algorithm?

View Replies View Related

Increment Counter By Recursion

Mar 22, 2015

I have a question related to the code below, that I do not understand. The aim is to count all files and subdirectories in an ArrayList full of files and subdirectories. So I have to count every file and every subdirectory.

The code concerning counting files is clear to me - every time d is of the type file I have to increment n by one. However I thought that I have to do the same thing in case d is a directory, so I would have written the same code for directories.

So what does "n += ((Directory) d).countAllFiles();" mean? In my understanding the method countAllFiles() is applied again on the object Directory ( as Directory is the class that contains this method), but how is n incremented by this? I thought n should be incremented by one as we did with files.

public int countAllFiles() {
int n = 0;
for(SystemFile d : content) {
if(d instanceof File) {
n++;

[Code] ....

View Replies View Related

How Recursion Calls Are Made

Jun 5, 2014

How the recursion works. I tried to figure out writing down low, mid, high at each recursive call. But I seem to be making a mistake somehow. I don't understand where the values are returned to in

if(leftmax>rightmax){
return leftmax;}
else
return rightmax;

Here's the code:

public class Maximum{
public static int max(int[] a,int low,int high){
int mid,leftmax,rightmax;
if(low==high)
return a[low];

[Code] ....

firstly leftmax=max(a,0,4)

Then what is the next line executed?Is it rightmax=max(a,5,8).

After this is it leftmax=max(a,5,6)

rightmax=max(a,7,8)

I tried to understand what these recursion calls by writing them down.But I somehow make a mistake.

View Replies View Related

Creating Palindromes Using Recursion?

Oct 20, 2014

I just started studying recursion and I wanted to know how to create a palindrome number going up from 1 to n then back to 1 like this: "12345...n...54321".

I've done one going downwards and then upwards like this: "n...4321234...n".

Here's my code:

Java Code: import java.util.*;
public class PalindromeTest
{
public static void downPalindrome(int n)

[Code].....

View Replies View Related

Printing Pattern Using Recursion

Jul 16, 2014

The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25.For example, if the value was 4, the pattern would look like this

*
* *
* * *
* * * *
* * *
* *
*

The values are stored in a file entitled prog3.dat which looks like this

4
3
15
26
0

I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem.Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.

import java.util.Scanner;
import java.io.*;
public class Program3 {
public static void main(String[] args) throws Exception {
int num = 0;
java.io.File file = new java.io.File("../instr/prog3.dat");
Scanner fin = new Scanner(file);

[code]...

It appears to be reading the file correctly, but is only printing the top half of the pattern. Also, like I said, I'm not very familiar with recursion, so am not sure if this is actually recursion?

View Replies View Related

Recursion Pattern In Java

Jul 16, 2014

The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25. For example, if the value was 4, the pattern would look like this
 
*
* *
* * *
* * * *
* * *
* *
*
 
The values are stored in a file entitled prog3.dat which looks like this
 
4
3
15
26
0
 
I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem. Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.
 
import java.util.Scanner; 
import java.io.*;
public class Program3 {
public static void main(String[] args) throws Exception {
int num = 0;
java.io.File file = new java.io.File("../instr/prog3.dat");
Scanner fin = new Scanner(file);

[Code] ....
 
Output:
 
Please enter an integer
*
* *
* * *
* * * *
Please enter an integer
*
* *
* * *
Please enter an integer
 
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
Please enter an integer
Please enter an integer
 
As you can see, I don't know how to make it print the pattern like in the example and am honestly not even sure if this is recursion since I've never actually worked with recursion before.

View Replies View Related

Java Recursion Word Pyramid?

Apr 26, 2015

I was asked to create a word pyramid program using recursion. I understand the concept of recursion (i.e. a method calling itself with the problem it is solving getting simpler and simpler each time) but I am having issues writing the program. Here is my code:

public static void main(String[] args) {
//This program will create a word pyramid by using a recursive algorithm.
//For example, the output of "DOGGY" should be:
//DOGGY
//OGG
//G
//This program will also use a recursive algorithm to accomplish this.
String userWord = JOptionPane.showInputDialog(null, "Hello and welcome to the Word Pyramid program."

[Code] .....

So, if I input HORSE the output should be:

HORSE
ORS
R

View Replies View Related

Create MergeSort Method Without Using Recursion

Nov 6, 2014

We are supposed to create a MergeSort method without the using recursion. Most of the code is already completed, the only thing that I believe I need are two for loops (an inner and an outter) that will make calls to the merge method. I need implementing the sort method of the merge sort algorithm without recursion, where the length of the array is a power of 2. Keep merging adjacent regions whose size is a power of 2. For ex: lengths will be 1, 2, 4, 8, 16,.

public class MergeSorter {
public static void sort(int[] a) {
//for(int i = 1; i <= a.length; i++) the parameters for the for loop are wrong.
{
merge(0,0,1,a);
merge(1,1,2,a);
merge(2,2,3,a);

[Code]...

View Replies View Related







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