2

I have new assignment and since I am new to JAVA I don't know how to make it work, I have searched this website frequently but all the solutions which were suggested didn't work out for me or I didn't use them properly, I would be grateful if someone helps me... the code below is the most simple solution I could find but still doesn't work...

I want to get inputs like names from people and change them to numbers (int)...it says it is not possible to cast from string to int ... !!

 package loveindex;
import java.util.Scanner;
//import java.math.BigInteger;

public class LoveIndex {

private static Scanner scan;

public static void main(String[] args) {
    scan = new Scanner(System.in);

    System.out.println("Testing Scanner, write something: ");
    String testi = scan.nextLine();
    System.out.println(testi);
    System.out.println("Testing Scanner, write something: ");
    String testi2 = scan.nextLine();
    System.out.println(testi2);

    int ascii = (int) testi;
    int ascii = (int) testi2;
  }

}
6
  • sorry I edited my question :( Commented Nov 3, 2014 at 22:40
  • What ascii character would you expect "Fnord" to convert to? Commented Nov 3, 2014 at 22:41
  • 2
    How do you want your strings converted to numbers? Give us some examples of how you expect this to work Commented Nov 3, 2014 at 22:43
  • Each character in a Java String is a 16-bit unsigned UTF-16 value. For "normal" characters (anything on a standard English keyboard) the character values are all less than 127 and map to the ASCII character set (Google it). You can use String's charAt(N) to retrieve the Nth character of the String as a char value, and that can be assigned to an int with no loss of information. Commented Nov 3, 2014 at 22:45
  • for example I get name like "John" and I am supposed to get the AScII value which has to be numbers i mean "integer" (the famous table thing) and later on I will use them in some equation to make love index.... Commented Nov 3, 2014 at 22:46

4 Answers 4

4

You Can Try This:

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    System.out.println("Testing Scanner, write something: ");
    String testi = scan.nextLine();
    char[] ascii1 = testi.toCharArray();

    for(char ch:ascii1){
        System.out.println((int)ch+"  ");
    }


    System.out.println("Testing Scanner, write something: ");
    String testi2 = scan.nextLine();
    char[] ascii2 = testi2.toCharArray();

    for(char ch:ascii2){
        System.out.println((int)ch+"  ");
    }

  scan.close();
}
Sign up to request clarification or add additional context in comments.

2 Comments

:: this works fine but could you tell me how to sum up the chars at the end? for example for a name "John" I get 4 int and I need to sum them or multiply them in different equations... but i dont know how to use them now :(
I found it ... tnx any way :P
2

Achieve the same in a concise way by employing Java 8's lambda function. From your comment (at the accepted answer) you need the sum of the characters at the end?

String str = "name";
int sum = str.chars().reduce(0, Integer::sum);

Comments

0

You're attempting to assign a String to an int which the 2 of which are incompatible. You can get the ascii value of a single character

int ascii = testi.charAt(0);

2 Comments

@HamedFazilat it will give the ascii of the first character in testi
@johny sorry but wuuuut ? u mean it is going to convert only the first letter like J from Johny ?
0

You cannot convert string to ascii like that. You can convert a character to ascii

int ascii = (int)some_char;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.