GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Oct-2009, 07:41
ZackCampbell5 ZackCampbell5 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
ZackCampbell5 is an unknown quantity at this point

Converting Octal to Decimal and then Decimal to Octal


I am a beginner in computer programing using java. I was given a project in my college level class to create a program to convert Octal numbers to decimal numbers and decimal numbers to octal numbers. I have specific guidelines that i have to follow but the only thing that i can not figure out is the coding for converting the numbers. If i could get information on how to do this conversion using Strings and StdIn and not using reader buffers, that would be a great help.

this is what i have so far:

JAVA Code:
public class Project2
{
  public static void main (String[] args)
  {
    int Option;
    String Octal;
    String Decimal;
    System.out.println ("1. Decimal to Octal");
    System.out.println ("2. Octal to Decimal");
    System.out.println ("3. Exit");
    System.out.println ("Enter Your Menu Seclection (1,2,3): ");
    Option = StdIn.readInt ();
    while (Option != 3)
    {
      if (Option == 1)
      {
        System.out.println ("Please enter your Decimal: ");
        Decimal = StdIn.readString ();     
      }
      else if (Option == 2)
      {
        System.out.println ("Please enter your Octal: ");
        Octal = StdIn.readString ();


      }
      else 
      {
        System.out.println ("-----Option " + Option + " does not exist. Please select option 1,2, or 3.-----");
        System.out.println ("1. Decimal to Octal");
        System.out.println ("2. Octal to Decimal");
        System.out.println ("3. Exit");
        System.out.println ("Enter Your Menu Seclection (1,2,3): ");
        Option = StdIn.readInt ();
      }
    }
    if (Option == 3)
    {
      System.out.println ("Thank You. Good bye.");
    }    
  }
}
Last edited by LuciWiz : 27-Oct-2009 at 07:46. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 27-Oct-2009, 20:46
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Converting Octal to Decimal and then Decimal to Octal


Quote:
Originally Posted by ZackCampbell5
...If i could get information on how to do this conversion using Strings...
See the Java's String class API for various parse-ing methods.
...and also check the decode() function of the Integer class.

Quote:
Originally Posted by ZackCampbell5
... and not using reader buffers, that would be a great help.
How about the Scanner class? [requires Java 1.5 or higher] See this example.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 28-Oct-2009, 07:18
ZackCampbell5 ZackCampbell5 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
ZackCampbell5 is an unknown quantity at this point

Re: Converting Octal to Decimal and then Decimal to Octal


Well i was told that i have Java 1.6 but when i tried to run the program that you suggested (with the scanner import) i only got this message:

Code:
java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ScannerTest.main(ScannerTest.java:30)

also i have done extensive research on the parse-ing method and do not understand how to code it.

is it possible to use some sort of array in the converting process?
>
  #4  
Old 29-Oct-2009, 17:06
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Converting Octal to Decimal and then Decimal to Octal


Huh?

The example runs fine for me:
Code:
C:\Documents and Settings\user\Desktop\OTH>"\Program Files\Java\jdk1.5.0_01\bin\javac.exe" ScannerTest.java C:\Documents and Settings\user\Desktop\OTH>"\Program Files\Java\jdk1.5.0_01\bin\java.exe" ScannerTest Enter int, double, word, line: 4 1.0 check this is only a test! 4:1.0:check: this is only a test!
...so I'm not sure what was done?
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 30-Oct-2009, 07:27
ZackCampbell5 ZackCampbell5 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
ZackCampbell5 is an unknown quantity at this point

Re: Converting Octal to Decimal and then Decimal to Octal


Sorry i had several erros while trying to run the scanner. It works fine now thank you but how will i use the scanner to help me convert from octal to decimal? sorry like i said before i am a beginner
  #6  
Old 30-Oct-2009, 08:37
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Converting Octal to Decimal and then Decimal to Octal


Quote:
Originally Posted by ZackCampbell5
... but how will i use the scanner to help me convert from octal to decimal?
I don't know what your "specific guidelines" might be, but here is a small example with the Scanner in use:
JAVA Code:
import java.util.Scanner;  // for Scanner class

public class HexOctExample
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        int aValue;

        System.out.print("Input a decimal number to convert: ");
        aValue = input.nextInt();

        System.out.println("The conversion:");
        System.out.println("...as HEX: " + Integer.toHexString(aValue)); 
        System.out.println("...as OCT: " + Integer.toOctalString(aValue)); 
    }
}
example interaction/output:
Code:
Input a decimal number to convert: 128 The conversion: ...as HEX: 80 ...as OCT: 200
HTH.

EDIT:
Note that the example doesn't have exception [try/catch] handling around the input retrieval, but at this point, you probably have NOT covered exceptions yet, so I left that out.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #7  
Old 03-Nov-2009, 07:30
ZackCampbell5 ZackCampbell5 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
ZackCampbell5 is an unknown quantity at this point

Re: Converting Octal to Decimal and then Decimal to Octal


That Scanner import did work successfully for me but i do not think that i am allowed to use that. I think that i am supposed to go the long way around it by using this sort of method in my coding :
921/8 = 115, 921%8 = 1
115/8 = 14, 115%8 = 3
14/8 = 1, 14%8 = 6
1/8 = 0, 1%8 = 1
it may seem like a waste of time to do each of these individual steps but i think that this is what my instructor requires. if you could somehow show me how to code this to make it less of a pain to convert like this every time. Thank you.
  #8  
Old 03-Nov-2009, 12:58
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Converting Octal to Decimal and then Decimal to Octal


Quote:
Originally Posted by ZackCampbell5
it may seem like a waste of time to do each of these individual steps but i think that this is what my instructor requires.
NOT a waste of time. This is fundamental knowledge as to converting to other base values using basic operators from the language. I recall those days too, although it seems to be a "waste of time" now, it's all part of the learning, and is needed when one ever encounters a situation where (based on the limits of the targeted programming environment) a library cannot be used and you have to write a small routines yourself to do what you need. Also, as a professor once told us, "It is learning the hard way that makes you appreciate the use of libraries."
Quote:
Originally Posted by ZackCampbell5
if you could somehow show me how to code this to make it less of a pain to convert like this every time. Thank you.
Hint: Looking at the example data you gave, it nearly cries for loop handling. Setting up a loop can "ease the pain" of individual steps!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:35.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.