[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java noob needs help with char variables

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 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