Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Get int value from char, char pulled from String

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Honduras
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get int value from char, char pulled from String

    I have a logic problem.

    My code is succesfully compiled, but when running it is not working the way it should.

    Briefly explained, I want my program to do a 'simple' add operation.
    However there are several conditions to it;
    You have to specify how many digits in a number are allowed to be able to add.
    And finally to recognize the character '=' or the value 61, to print the result.

    However, program is not recognizing the value of the character.

    Anyone knows what i'm doing wrong?

    /** Un programa que pida un numero de 3 digitos y sume el total */
    import java.util.Scanner;
     
    public class SumaAvanzada{
     
        public static void main(String[] args ){
     
    	Scanner sc = new Scanner(System.in);
     
    	// Variables e Inicializacion:
    	int filtro;
    	String input = "";
    	int numerodigit;
    	char digit;
    	int bloc;
    	int totalnum;
    	int total;
    	totalnum = 0;
    	total = 0;
    	int switchoff;
    	switchoff = 0;
     
    	// Obtiene el string y el numero de digitos permitidos:
     
    	System.out.println( " Ingrese el numero de digitos permitidos: " );
    	filtro = sc.nextInt();
     
    	System.out.println( " Sumar: " );
     
    	do{
     
    	input = sc.next();
     
    	// Obtiene el numero de digitos de la String:
    	numerodigit = input.length();
     
    	// Compara el numero de digitos del string vs. el filtro designado:
    	if ( numerodigit == filtro ){
     
    	    // Lee cada char del string una a la vez.
            for ( int e = 0; e < numerodigit; e++ ){
            digit = input.charAt(e);
    		int digitval = (int) digit;
     
    		    // Determina la posicion del digito.
    		    int A = numerodigit - e;
     
    		    //  Si el char tiene un valor de 0 a 9...
    		    if ( digitval >= 0 && digitval <= 9 ){
     
    			    // Multiplica por multiplos de 10 correspondiente a la posicion del digito... 
    				bloc = 1;
    				do{ bloc = bloc * 10; A--; } while ( A > 0 );
    				bloc = bloc * digitval;
     
    			// y suma el resultado.
    		    totalnum = totalnum + bloc;
    			total = total + totalnum;
     
                }			
     
    			else  
     
    			    if ( digitval == 61 ){ System.out.println( total ); switchoff = switchoff + 1;} 
     
     
    				    else System.out.println ( "Ingrese un numero valido o el symbolo \' = \' ." );
    					;
    			;
     
    		}
    	}
     
    	else 
     
    	    System.out.println ( "Ingrese un numero valido o el symbolo \' = \' 2." );
     
    	}while ( switchoff == 1 );
     
    	}
     
    }
    Andrew R.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Get int value from char, char pulled from String

    program is not recognizing the value of the character.
    Can you explain where the problem is? What character's value is not being recognized?
    Do you understand that the value of '3' is not the same as the int: 3
    See the ASCII character table for a description of character values.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get int value from char, char pulled from String

    Try the following modification

    	// Lee cada char del string una a la vez.
            for ( int e = 0; e < numerodigit; e++ ){
            digit = input.charAt(e);
    		int digitval = Character.digit(digit, 10)

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Location
    Honduras
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get int value from char, char pulled from String

    @ Norm,

    I do you understand that the value of '3' is not the same as the int: 3
    I know about the ASCII table.

    What I am ultimately trying to achieve is to get the int value of a char.

    @ mperemsky,

    I'll try it.
    Andrew R.

Similar Threads

  1. string-int-char
    By mdhmdh100 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 25th, 2012, 01:35 PM
  2. How to check if a user input is a char or a int
    By Blackbird94 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 8th, 2011, 04:17 PM
  3. char to int cov problem
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 16th, 2011, 11:40 AM
  4. converting int to char
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 19th, 2011, 12:59 AM
  5. [SOLVED] Why do i need to take 48? char to int.
    By Scotty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 29th, 2010, 10:23 PM