Solved

Basic Text Input

Posted on 2003-11-21
11
330 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 47
login jsp example 24 85
difference between sorce folder and folder in eclipise 3 40
maven disable workspace resolution 1 37
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 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 …
This video teaches viewers about errors in exception handling.

756 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