Solved

java noob needs help with char variables

Posted on 2003-11-29
4
560 Views
Last Modified: 2012-08-13
right im new to java and was wondering if the following was possible:-

if i declare a variable as char type i can only read in one charactor. is it possible to make it read in 2 charactors?

if thats not possible. can one limit the length of a string to say two?

what im trying to do in my program is that when the user types in 'abcdefg' i just want to take the first two letters an nothing else.

***for the char input***
char res;
            
System.out.println("enter ");
res=KBInput.readChar();
            
System.out.println(res);

Cheers!!

Jack
***method for KBInput***
public final static char readChar()
  {
    char ch = 0;
    BufferedReader inp =
      new BufferedReader(new InputStreamReader(System.in));
    try
    {
      ch = (char)inp.read();
    }
    catch (IOException e)
    {
      System.out.println("Error "+e.toString());
    }
    return ch;
  }

***for string input***
String res;
            
System.out.println("enter ");
res=KBInput.readString();
            
System.out.println(res);
***String method***
public static String readString()
  {
    String inputString;
    try
    {
      BufferedReader inp =
      new BufferedReader(new InputStreamReader(System.in));
      return inp.readLine();
    }
    catch (IOException e)
    {
      System.out.println("Error "+e.toString());
      return " ";
    }
  }

Cheers!!

Jack
0
Comment
Question by:skinnyl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 9842283
All you need to do is read a String and then cut it:

String s = readString();
if (s.length() > 2) {
      s = s.substring(0, 3);
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9842289
Then, if you want to access the chars individually:

char[] chars = s.toCharArray();
char c1 = char[0];
char c2 = char[1];
0
 

Author Comment

by:skinnyl
ID: 9842354
wonderful mate!!

thanks

Jack
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9842356
8-)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question