8 Replies - 141 Views - Last Post: 27 June 2020 - 07:57 AM Rate Topic: -----

#1 baki2001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-June 20

How to add an indicator next to a specific value in recursion

Posted 26 June 2020 - 12:59 PM

I am trying to add a simple arrow next to Least Significant Octal digits(LSD) and Most Significant Octal digits(MSD). My code turns simple integers into Octal numbers and I am using recursion to do so. The MSD and LSD are the first and last remainders of Division in the list. On top of that. I don't know how to align them in the output like in the example below; I need help with that too.

This is what I want my output to look like:

4215 / 8 = 526.8 = 526 R 7 <==== LSD
526 / 8 = 65.75 = 65 R 6
65 / 8 = 8.125 = 8 R 1
8 / 8 = 1.000 = 1 R 0
1 / 8 = 0.125 = 0 R 1 <==== MSD

This is the code I have so far which only does the calculations and returns the values:

public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter any digit");
int input = scan.nextInt();
System.out.println(dec2oct(input));
}

public static String dec2oct (int dec)
{
ArrayList <String> x = new ArrayList<String>();

if (dec == 0)
{
return ("");
}
else
{
double div = dec /(double) 8 ;
int rem = dec % 8;
int r = dec / 8;
System.out.println(dec+" / 8 = "+div+" = "+r+" R "+rem);
return ("" + dec2oct®);

}
}

Is This A Good Question/Topic? 0
  • +

Replies To: How to add an indicator next to a specific value in recursion

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: How to add an indicator next to a specific value in recursion

Posted 26 June 2020 - 01:05 PM

What is the output from the current code? Can you copy and paste it here?

Quote

how to align them in the output

Have you looked at the printf method? It allows you to control the output formatting.

Also please wrap all posted code in code tags: Select the code and press the [CODE] button

This post has been edited by NormR: 26 June 2020 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 baki2001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-June 20

Re: How to add an indicator next to a specific value in recursion

Posted 26 June 2020 - 01:17 PM

public static void main (String [] args)
    {
        Scanner scan = new Scanner(System.in);
        int input = scan.nextInt();
        System.out.println("Enter any digit");
        System.out.println(dec2oct(input));
    }
    
    public static String dec2oct (int dec)
    {
        
        
        if (dec == 0)
        {
            return ("");
        }
        else
        {
            double div = dec /(double) 8 ;
            int rem = dec % 8;
            int r = dec / 8;
            System.out.println(dec+" / 8 = "+div+" = "+r+" R "+rem);
            return ("" + dec2oct(r));
            
        }
    }



Current Output :
4215 / 8 = 526.875 = 526 R 7
526 / 8 = 65.75 = 65 R 6
65 / 8 = 8.125 = 8 R 1
8 / 8 = 1.0 = 1 R 0
1 / 8 = 0.125 = 0 R 1

Output Required:
4215 / 8 = 526.875 = 526 R 7 <=== LSD
526 / 8 = 65.75 = 65 R 6
65 / 8 = 8.125 = 8 R 1
8 / 8 = 1.0 = 1 R 0
1 / 8 = 0.125 = 0 R 1 <==== MSD

I want the numbers to be formatted as well
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: How to add an indicator next to a specific value in recursion

Posted 26 June 2020 - 01:18 PM

Look at the printf method (instead of println) for doing formatting.

This post has been edited by NormR: 26 June 2020 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 baki2001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-June 20

Re: How to add an indicator next to a specific value in recursion

Posted 26 June 2020 - 01:19 PM

i know i have to use the print formatting but i don't know how to implement it to make it all aligned especially with the LSD and MSD arrows
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: How to add an indicator next to a specific value in recursion

Posted 26 June 2020 - 01:21 PM

Quote

i have to use the print formatting

What have you tried? What happened?
Was This Post Helpful? 0
  • +
  • -

#7 baki2001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-June 20

Re: How to add an indicator next to a specific value in recursion

Posted 26 June 2020 - 01:30 PM

I haven't tried anything with the formatting or the arrows yet I don't know where to start
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: How to add an indicator next to a specific value in recursion

Posted 26 June 2020 - 02:06 PM

Quote

where to start

Look at the API doc for the java.util.Formatter class. It defines the format control Strings that would be used with the System.out.printf method. Write a small test program with test formatting Strings and values to see how to use the printf method. When you get that to work, then merge it in with your code.
I am sure there are lots of examples online if you ask Google for some samples.

The API doc for java SE classes is here: https://docs.oracle..../api/index.html

This post has been edited by NormR: 26 June 2020 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: How to add an indicator next to a specific value in recursion

Posted 27 June 2020 - 07:57 AM

Quote

 ArrayList <String> x = new ArrayList<String>();

You have two obvious options:

a. pass that List as a parameter to the method and add the string currently being printed to it
b. keep the List as a class or instance variable and do the above

In either case, simply append MSD/LSD to the first and last items in the List
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1