Solved

java noob needs help with char variables

Posted on 2003-11-29
4
556 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
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
wonderful mate!!

thanks

Jack
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now