Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Basic Text Input

Posted on 2003-11-21
11
Medium Priority
?
339 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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 800 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

715 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