-3

Really stupid question, but what's the relevant to integer for letters. This is an example - when I write 1, the console prints 2. What I want to do is instead of using numbers, to use letters. For example when I write "a" it prints "b", as int num only allows me to use numbers .. for obvious reasons. Sorry about the question again, really new into Java.

int num1;
 Scanner input = new Scanner(System.in);
     System.out.println("First number");
     num1 = input.nextInt();

 if (num1 == 1);
 {
     System.out.println("2");
 }
Mr_Pouet
  • 4,061
  • 8
  • 36
  • 47

2 Answers2

3

The JavaDocs are really helpful in situations like this (google "javadoc Scanner" for example). If we look through the methods provided by scanner we find nextInt() which you already know about, but we also find next() which returns a String and says it "Finds and returns the next complete token from this scanner." Well, that seems helpful, next() does seem to be the kind of thing we are looking for.

String s = input.next();

Now, how to get at the first character in s. You guessed in (google "javasoc String"). Right at the top we find charAt() which seems perfect:

char c = s.charAt(0);

now you can see what is in c:

if (c == 'a') {
    /* do something */
}
John Hascall
  • 9,176
  • 6
  • 48
  • 72
2

if you can be more clear ı can help but this is what ı understood about your question. hope it helps

    String str;
    Scanner input = new Scanner(System.in);
    System.out.println("First letter ");
    str = input.next();

    if (str.equals("a"))

    {
        System.out.println("b");

    }
berione
  • 122
  • 10