Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Inserting Multiple Lines from Console (http://www.velocityreviews.com/forums/t959539-inserting-multiple-lines-from-console.html)

subhabangalore@gmail.com 04-08-2013 01:59 PM

Inserting Multiple Lines from Console
 
Dear Group,

I was trying to get input of different nature of string, int etc.

To insert multiple lines I can do sort of standard coding like,

public class BRReadLines {
public static void main(String args[]) throws IOException
{
// Create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'end' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while(!str.equals("end"));
}
}

I may assign string and int also. But I was looking to assign different variable names like,
String str;
int In;
.....
and read from console.

Initially I did
str=br.readLine()
and
In=br.read()

but did not help much,
then I am trying like,

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));

str=br.readLine();
In=br1.read();

It is generally going okay but when the output is being generated I am getting arbitrary output.

If anyone can kindly suggest how may I improve it?

Regards,
Subhabrata.

Stefan Ram 04-08-2013 02:06 PM

Re: Inserting Multiple Lines from Console
 
subhabangalore@gmail.com writes:
>If anyone can kindly suggest how may I improve it?


public class Main
{
public void quit(){ java.lang.System.exit( 0 ); }

public void main() throws java.lang.Throwable
{ final javax.script.ScriptEngine scriptEngine
= new javax.script.ScriptEngineManager().getEngineByName ( "JavaScript" );
scriptEngine.put( "main", this );
final java.lang.StringBuilder text = new java.lang.StringBuilder();
scriptEngine.put( "text", text );
scriptEngine.eval( "print('');" ); // loads engine for faster reaction
java.lang.System.out.printf
( "*** Texteditor 2000, V1.0 ***%n%n %d Bytes Free%n%n Enter%n%ntext." +
"append('example');%n%n or other StringBuilder calls in JavaScript " +
"syntax to edit,%n%n or enter%n%nmain.quit();%n%n to quit.%n%n",
java.lang.Runtime.getRuntime().freeMemory() );
while( true )try
{ java.lang.System.out.printf( "%n> ", text );
java.lang.System.out.println
( scriptEngine.eval( System.console().readLine() ));
java.lang.System.out.printf( "%s%n", text ); }
catch( final java.lang.Throwable throwable )
{ java.lang.System.err.println( throwable ); }}

public static void main( final java.lang.String[] args )
throws java.lang.Throwable { new Main().main(); }}


Joerg Meier 04-08-2013 03:04 PM

Re: Inserting Multiple Lines from Console
 
On 8 Apr 2013 14:06:21 GMT, Stefan Ram wrote:

> subhabangalore@gmail.com writes:
>>If anyone can kindly suggest how may I improve it?

> public void quit(){ java.lang.System.exit( 0 ); }


Aside from the horrible, horrible JavaScript advice that I honestly pray
you posted as a joke, why are you qualifying java.lang things ?

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

markspace 04-08-2013 04:56 PM

Re: Inserting Multiple Lines from Console
 
On 4/8/2013 6:59 AM, subhabangalore@gmail.com wrote:

> I was trying to get input of different nature of string, int etc.


> It is generally going okay but when the output is being generated I
> am getting arbitrary output.


I think first your main problem is trying to define the problem. You
need to define what "nature" you want to read in what circumstances.
Nobody just reads lines and turns them into strings or ints on a whim.

If you need some definition because your just doing this on your own,
consider writing a simple parser for algebraic input. Parsers are
common in computer science, so they're good to know about, look it up.

The next step is to define some test cases. Here's some lines you
should be able to read in and parser correctly. Note that I'm assuming
you'll do this "algebraically" and that * and / get evaluated before +
and -.

1 + 1
-10
1243 + 678 * 5
1 / 3
45 - 34 + 12 - 90 + 78 * 3 / 2

Daniel Pitts 04-08-2013 09:10 PM

Re: Inserting Multiple Lines from Console
 
On 4/8/13 8:04 AM, Joerg Meier wrote:
> On 8 Apr 2013 14:06:21 GMT, Stefan Ram wrote:
>
>> subhabangalore@gmail.com writes:
>>> If anyone can kindly suggest how may I improve it?

>> public void quit(){ java.lang.System.exit( 0 ); }

>
> Aside from the horrible, horrible JavaScript advice that I honestly pray
> you posted as a joke, why are you qualifying java.lang things ?

The fully qualifying of classes, as well as the strange indentation, is
typical of Stefan. I usually ignore his code snippets, but do try to
comprehend his non-code.


Lew 04-08-2013 10:10 PM

Re: Inserting Multiple Lines from Console
 
Daniel Pitts wrote:
> Joerg Meier wrote:
>> Stefan Ram wrote:
>>> subhabangalore@... writes:
>>>> If anyone can kindly suggest how may I improve it?

>
>>> public void quit(){ java.lang.System.exit( 0 ); }

>
>> Aside from the horrible, horrible JavaScript advice that I honestly pray


Yes, that is the really strange thing there.

>> you posted as a joke, why are you qualifying java.lang things ?


He's pretty aggressive about refusing to comply with community standards.

> The fully qualifying of classes, as well as the strange indentation, is
> typical of Stefan. I usually ignore his code snippets, but do try to
> comprehend his non-code.


In this case it's a bizarre answer.

The suggestion to use Scanner or a string tokenizer was better.

--
Lew

znôrt 04-08-2013 11:08 PM

Re: Inserting Multiple Lines from Console
 
Lew <lewbloch@gmail.com> writes:

> In this case it's a bizarre answer.


i found it amusing, and quite brilliant.

> The suggestion to use Scanner or a string tokenizer was better.


instead of pointing to some basic building blocks with which to build a
rudimentary parser, he presented a working solution using a complete
parser already available to java. i would definitely hire this guy! xD

and his code style isn't that bad. anyway if your eyes start to bleed
just paste into eclipse and hit ctrl-shift-f!

Lew 04-09-2013 12:59 AM

Re: Inserting Multiple Lines from Console
 
znrt wrote:
> Lew writes:
>> In this case it's a bizarre answer.

>
> i [sic] found it amusing, and quite brilliant.


This is a Java newsgroup, and the OP clearly was trying to learn Java.

>> The suggestion to use Scanner or a string tokenizer was better.

>
> instead of pointing to some basic building blocks with which to build a
> rudimentary parser, he presented a working solution using a complete
> parser already available to java [sic]. i [sic] would definitely hire this guy! xD


Not appropriate for the question, though.

> and his code style isn't that bad. anyway if your eyes start to bleed
> just paste into eclipse [sic] and hit ctrl-shift-f!


You don't work in a professional programming environment with source control and
code reviews, do you?

Formatting changes introduce more to review and get wrong. They are frownedupon.

Everywhere I've worked has insisted on a house style before checkin is permitted.

Also, I program on a Mac a lot of the time, so Ctrl-Shift-F doesn't work, and when at
home I usually use NetBeans, which has a different key chord.

Not everyone uses Eclipse or NetBeans, either.

Stefan is notable for his attitude about conventional formatting and simpletype names.

--
Lew


znôrt 04-09-2013 06:46 AM

Re: Inserting Multiple Lines from Console
 
Lew <lewbloch@gmail.com> writes:

>> he presented a working solution using a complete

> Not appropriate for the question, though.


he did show some functionality that java has out of the box, and how to
use it in a smart and effective way.

> You don't work in a professional programming environment with source
> control and code reviews, do you?


i do. hell i do! and standups and retrospectives and breakfasts and
coding dojos and checkstyles and freaking sonar pedantic warnings are my
everyday's cross to bear. and let's not forget common criteria! that's
why i can appreciate some fresh air sometimes. and some bizarre-ness for
a change, why not :-)

> Formatting changes introduce more to review and get wrong. They are
> frowned upon.


indeed. i didn't suggest raping any house rules either.

> Everywhere I've worked has insisted on a house style before checkin is
> permitted.


i have to assume you wouldn't work in a shop run by stefan xDDD

> This is a Java newsgroup, and the OP clearly was trying to learn Java.


you have a point there. and it's nice that someone pointed out that
stefan's contribution is unconventional, but then there is still some
value to it i wouldn't dismiss just because it is unconventional or is
something ... well, that would be most probably frowned upon in a
conventional sw engineering dept.

Arved Sandstrom 04-09-2013 08:39 AM

Re: Inserting Multiple Lines from Console
 
On 04/08/2013 09:59 PM, Lew wrote:
> znrt wrote:
>> Lew writes:
>>> In this case it's a bizarre answer.

>>
>> i [sic] found it amusing, and quite brilliant.

>
> This is a Java newsgroup, and the OP clearly was trying to learn Java.
>
>>> The suggestion to use Scanner or a string tokenizer was better.

>>
>> instead of pointing to some basic building blocks with which to build a
>> rudimentary parser, he presented a working solution using a complete
>> parser already available to java [sic]. i [sic] would definitely hire this guy! xD

>
> Not appropriate for the question, though.
>
>> and his code style isn't that bad. anyway if your eyes start to bleed
>> just paste into eclipse [sic] and hit ctrl-shift-f!

>
> You don't work in a professional programming environment with source control and
> code reviews, do you?
>
> Formatting changes introduce more to review and get wrong. They are frowned upon.


The problem there sounds more like unclear ownership of source code. The
move away from formal checkout-checkin in source control systems
(although oddly enough not in many other systems, like CMS or ECM)
doesn't mean that there shouldn't be _informal_ checkout-checkin, i.e.
at any given time only certain individuals should have "ownership" of a
file, optimally one person per file per branch.

If this informal policy is observed, formatting changes accompany real
changes, and sometimes the formatting changes *are* the real changes.
Basically this kind of "known ownership" goes a long way to eliminate
dabbling, including casual formatting tweaks.

> Everywhere I've worked has insisted on a house style before checkin is permitted.


I've worked in places that had a "house style" and enforced it, places
that said they had one but didn't enforce it, and on projects that
didn't have one at all. I've found that the need for a house style
diminishes if coders know not to dabble and nitpick, they know only to
do a format if it's an intentional _refactoring_ of sorts to improve
maintainability, and the individual styles of people are good (IOW, good
coders).

To the latter point, I have no problem working with another programmer's
style if it's clear and makes sense. If I have been called upon to make
minor mods, I adopt the style.

> Also, I program on a Mac a lot of the time, so Ctrl-Shift-F doesn't work, and when at
> home I usually use NetBeans, which has a different key chord.
>
> Not everyone uses Eclipse or NetBeans, either.
>
> Stefan is notable for his attitude about conventional formatting and simple type names.
>

AHS


All times are GMT. The time now is 06:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.