?
Solved

java noob needs help with char variables

Posted on 2003-11-29
4
Medium Priority
?
563 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

649 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