Jump to content

Checking if input character is int

- - - - -

  • Please log in to reply
8 replies to this topic

#1
skypower

skypower

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
After a lot of googling and trying to figure out how to check if an input character is int, I give up. I wanna cheat now :D

Here's what I want the program to do:

Say I get this input: dasda2h
I want the program to output dasdahhh. This is because there is a 2 before the char in its right, it's pretty simple.

The way I try to do this is to check the variable of type char to check if it's keeping an integer, and if so just print the next letter this number of times.

Now on the main problem, I have tried many options to input (system.in.read, stdin.read(), nextInt()) but no luck. I have also been playing with Integer.parseInt() without any luck too.

Here's the code:
import java.io.*;
import java.util.Scanner;

public class Main {

    
    public static void main(String[] args) {

     Assign2 output = new Assign2();
     InputStreamReader convert = new InputStreamReader(System.in);
     BufferedReader stdin = new BufferedReader(convert);
     Scanner scan = new Scanner(System.in);
     char inp =' ', prev =' ', temp;
     log("Gief: ");
    try {
          prev = inp ;
          temp =(char)  stdin.read();
//          temp = (char) System.in.read();
            inp = temp;
           log("inp: "+inp+" temp: "+temp);
     }
     catch(IOException e) {}
     boolean retVal = false;
           try {
           Integer.parseInt(eisodos);
           retVal = true;
           }
           catch (NumberFormatException nFE) {}
    }



    public static void log(String message) {
     System.out.println(message);
    }


#2
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
In the Character class, there are methods that can determine if a character is a letter or a digit. You might want to try isDigit(), isLetterOrDigit(), or isLetter(). For more information, you can look in the Character class' API.

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think your first problem is that the scanner methods return String, and not char.
(In case you didn't know, char is for 1 single character only, String a series of characters, like a word, sentence or more text)
Better take the input as String. Then loop trough the String's characters.

String input;

input = scanner.readLine();


for(char character : input.toCharArray() ){

  //do something with character

}



#4
skypower

skypower

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Thank you both of you for your replies, but the problem persists. When I try to use the isdigit, isletter and isletterordigit methods I still get the "char/int cannot be dereferenced". I even create new char and int variables (to test if I am doing something wrong in input) and I still get the error.

int n=5;
char c='s';
c.isLetter();
n.isLetterOrDigit();


#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
That's because char, and every other class that doesn't start with a Capital letter aren't objects of classes and don't have methods. What you want to do is:

char c='s';

char n='5';


Character.isLetter(c);

Character.isLetterOrDigit(n);



#6
skypower

skypower

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts

wim DC said:

That's because char, and every other class that doesn't start with a Capital letter aren't objects of classes and don't have methods. What you want to do is:
char c='s';
char n='5';

Character.isLetter(c);
Character.isLetterOrDigit(n);

Thanks a lot, this is working!
By the way I don't fully understand what you mean by what you said.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, every class in Java starts with a Capital letter, like String, Scanner, File
(You should also create your own classes with a capital letter)

Except a few like int, byte, char, long, float, double,...
That's because they are so called "primitive types". These don't have any methods, and you also can't do "new" with it:

int n = new int();

This code fails, because int is no class.

If you ever feel the need to use a method of such a class. There are always the wrapper classes: Integer, Byte, Character, Long, Float, Double, ...
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

#8
skypower

skypower

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts

wim DC said:

Well, every class in Java starts with a Capital letter, like String, Scanner, File
(You should also create your own classes with a capital letter)

Except a few like int, byte, char, long, float, double,...
That's because they are so called "primitive types". These don't have any methods, and you also can't do "new" with it:
int n = new int();
This code fails, because int is no class.

If you ever feel the need to use a method of such a class. There are always the wrapper classes: Integer, Byte, Character, Long, Float, Double, ...
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

I know that each class should start with a capital letter, but how does it associates with c.isLetter() and Character.isLetter©. I just failed in using the method :)

#9
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
You can use Java's autounboxing feature to go between char and Character. Here is the oracle article on it. Autoboxing

Here's an example in code as well.

char xChar = 'x';

Character xObj = new Character(xChar);


if(xObj.isLetter())

   System.out.println(" 'x' is a letter");

else

   System.out.println(" 'x' is not a letter");


This code will output " 'x' is a letter." First, a primitive type char is created with the value x. In line two, a Character object is created, with the value of 'x'. Because 'x' is now stored in a Character object, you can use the methods in the class.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users