Solved

java noob needs help with char variables

Posted on 2003-11-29
4
557 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
starter POM and spring-boot-starter,  spring-boot-web 2 54
maven project error 5 54
configure dependency in POM for new database 3 25
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

776 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