Solved

Basic Text Input

Posted on 2003-11-21
11
337 Views
Last Modified: 2010-03-31
For some very easy pints could someone tell me what the basic command is to read text entered and put it in a variable.
ie when i print out

System.out.println("Enter subject name: ");

I want to put it in a string variable called subject. How do I read it into the variable? Pathetically simple i know but it's wreckin my head and i can't find any question as simple as this on the website anywhere. Thanks
0
Comment
Question by:liamgannon
[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
  • 6
  • 4
11 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 9800044
You can use System.in and do a read to get an array of bytes, and then convert that to a string:

byte chars[]=new byte[250];
int num=System.in.read(chars);
String input=new String(chars,0,num);
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9800168
an example code


*******
import java.io.*;


    public class ReadKeys {


        public String readKeys() {
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(": ");
        String name = null;


            try {
            name = console.readLine();
        }


            catch (IOException e) {
            name = "<" + e + ">";
        }
        return name;
    }


        public static void main(String[] args) {
        ReadKeys obj = new ReadKeys();
        System.out.println("You entered '" + obj.readKeys() + "'");
    }
}
*******

Source
http://www.planet-source-code.com/
0
 

Author Comment

by:liamgannon
ID: 9800710
Is there no simpler way of reading in like readline() or something similar of just one line?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:applekanna
ID: 9800720
isnt this what you were asking? ;)

name = console.readLine();
0
 

Author Comment

by:liamgannon
ID: 9800737
I thought that there was something simpler than a buffered reader, something just on one line that works the same way as println. The way that you suggested will work but i wanted something simpler that does't require a new class or a buffered reader. Is there no basic io api or somethin?
0
 

Author Comment

by:liamgannon
ID: 9800762
Surely there must be a simpler way of reading in without having to use a try and catch block each time
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9800873
AFAIK this is as small the programm can get

import java.io.*;

    public class ReadKeys1 {

        public static void main(String[] args)  throws IOException{
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter text for subject :" );
        String subject = null;
        subject = console.readLine();
        System.out.println("You entered '" + subject + "'");
    }
}
0
 
LVL 3

Accepted Solution

by:
applekanna earned 200 total points
ID: 9800877
AFAIK this is as small the programm can get

import java.io.*;

    public class ReadKeys1 {

        public static void main(String[] args)  throws IOException{
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter text for subject :" );
        String  subject = console.readLine();
        System.out.println("You entered '" + subject + "'");
    }
}

of the 4 lines 2 are System.out statemnts

Hope this helps
Cheers!
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9800900
cooment for Complete Reference for java by Herbert Schildt and patrick naughton

"Java does not have a generalized console input that parallels the standard C function scanf() or C++ input operators"

:)

Hope this clarifies your question.
Cheers!
0
 

Author Comment

by:liamgannon
ID: 9800903
Thanks, that cleared it up and the code works fine. Thanks again
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9800925
thx for the points :)
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

630 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