#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Converting a Char to an Int (new to java ) please help


    [code] int total = 0;
    int position = 0;



    System.out.println("please enter an octal number to convert to decimal ");
    String octal = user_input.next();

    int length = Integer.parseInt(octal);


    do {

    //total = (int) Math.pow(8,length);
    //length = length - 1;
    //System.out.println(total);
    Code:
            	int	digit = octal.charAt(position);
            	System.out.println(digit);
            	position = position +1;

    }while (position < length);

    }

    } [code]
    i am trying to print out the characters in the string that i enter 1 by 1 from left to right , and also i need to be able to multiply each character by 8 , do the chracters need to be converted to integers to do this ? and if so how do i do that ? im new to java and have never done any other type of language , so all this is very new to me .The code i am having trouble with is in green . thnkyou
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    char variables can be treated like int variables: 'c' + 1 = 'd'
    Not sure what character set multiplying them by 8 will get. ASCII characters range in value from 0 to 127

    Please edit the post and wrap the code in code tags:
    [code]
    ........ your code here.........
    [/code]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    char variables can be treated like int variables: 'c' + 1 = 'd'
    Not sure what character set multiplying them by 8 will get. ASCII characters range in value from 0 to 127

    Please edit the post and wrap the code in code tags:
    [code]
    ........ your code here.........
    [/code]
    yes but how do i print out the string characters from position 0 to the end of the string length? lets say string octal = 45678
    i then need the program to print out the chars at each position 1 by 1
    4
    5
    6
    7
    8
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    print out the chars at each position
    The String class has some methods you can use to get at its contents one character at a time.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    The String class has some methods you can use to get at its contents one character at a time.
    yes i have manages to do it now ,i used charAt() and then character.getNumericValue , but i think now my equation of coverting octal to decimal is wrong because im getting the wrong output. Are ther any good websites that explain the equation in a simple way ?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    import java.util.Scanner;


    public class ArrayString {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner user_input = new Scanner(System.in);
    [code]
    int endtotal = 0;
    int result = 0;
    int total = 0;
    int position = 0;


    System.out.println("please enter an octal number to convert to decimal ");
    String octal = user_input.next();

    int length = octal.length();


    do {

    result = (int) Math.pow(8,length);
    length = length - 1;

    char number = octal.charAt(position);
    int digit = Character.getNumericValue(number);

    total = digit * 8;


    position = position +1;

    int answer = total + result;


    endtotal = endtotal + answer;

    }while (1 < length);
    System.out.println(endtotal);
    [\code]
    }

    }

    This is the code that i have done so far , but the endtotal isnt what its suposed to be :/
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    the endtotal isnt what its suposed to be
    Please post the program's output and add some comments saying what is wrong with it.

    Please edit the post and wrap the code in code tags.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    Please post the program's output and add some comments saying what is wrong with it.

    Please edit the post and wrap the code in code tags.
    Code:
     int endtotal = 0;
    		 int result = 0;
    	     int total = 0;
    	     int position = 0;
    		
    				
    		System.out.println("please enter an octal number to convert to decimal ");
    		String octal = user_input.next();
    		
           int	length = octal.length();
          
           
            do {
        	  
               result =  (int) Math.pow(8,length);
               length = length - 1;
               
            	char number = octal.charAt(position);
            	int digit = Character.getNumericValue(number);
            	
            	total = digit * 8;
            	
            	
            	position = position +1;
            	
            	int answer = total + result;
            			
            	
            	endtotal = endtotal + answer;
            
            }while (1 < length);
        		  System.out.println(endtotal);
    	}
    please enter an octal number to convert to decimal
    345 <----- octal number to convert
    632 <-------- endtotal ( is wrong )


    345 is not 632 in decimal !
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    What is the correct answer?

    Describe the algorithm you are using to convert a String of char representing an octal number to an int value.

    For example given the String: "345"
    the result would be the sum of:
    3 * 8^2
    4 * 8^1
    5 * 8^0

IMN logo majestic logo threadwatch logo seochat tools logo