Solved

Basic Text Input

Posted on 2003-11-21
11
326 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
  • 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now