Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

is there a quicker way to read in input besides bufferedreader?

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess i could use a method or put it in a variable or something
but i'm just curious is there some other way to read input without using it at all!

also i have a question about Switch
if i want to evaluate the String or char input...what could i use
switch doesn't take these types, only ints and enum
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About your first question: Do you mean to read input from the console window (i.e. ask the user to input a value)? If you're using Java 5 or newer, have a look at the class Scanner (in package java.util). A quick example:

Your second question: Indeed, switch does not work with chars or strings. You can use a sequence of if ... else if ...:
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am on java 1.4 i think
and I figured using If/Else was the only way to look thru strings inputs
thanks for that Scanner heads up
how is that different from BufferedReader?
 
Marshal
Posts: 74757
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Jesper Young is correct. You would find Scanner easier to use, so I recommend you download a more recent version of Java. Remember Java 5 came out in Autumn 2004.

But is there a difference from BufferedReader? No, (if I remember correctly) Scanner is a method of encapsulating BufferedReader objects in an easy-to-use wrapper.

An alternative but more complicated way of getting different messages would be to put them as "values" into a HashMap, and use the names as "keys." Something like this:-
You can put "Alice," "Brenda," and "Carol" as keys, then when the HashMap sees "Alice" it returns whichever message you put there after "Alice." Putting a message with %s in into a printf() argument list will cause printf to replace the "%s" with the name.
You can also put several names with the same message.

But as I said it is a more complicated way to organise your choices, so I shall understand if you don't use it.
CR
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SDK I use is Eclipse. I they think they are all 1.4 versions. I may be wrong though.
 
Campbell Ritchie
Marshal
Posts: 74757
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can still download and install J6.
Then in Eclipse, right-click the name of the project->properties->Java compiler->project-specific settings->compiler compliance level->6.0 (or 5).
There is somewhere (I forget where) which allows you to set J6 as the default compliance.

This doesn't work if you have been told to use J1.4.2 (which I hope you haven't).
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[da]: also i have a question about Switch
if i want to evaluate the String or char input...what could i use
switch doesn't take these types, only ints and enum


That's not true - a switch statement can take a char, as well as byte, short, int, or enum types.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dawud alexander:
The SDK I use is Eclipse. I they think they are all 1.4 versions. I may be wrong though.


Eclipse is not an SDK, it's an IDE (Integrated Development Environment). Eclipse doesn't come with a JDK (Java Development Kit) or JRE (Java Runtime Environment). You have to download and install a JDK or JRE yourself (which you have done, otherwise you couldn't run Eclipse, because it is itself written in Java).

Eclipse runs on Java 1.4 or newer - so if you're really using Java 1.4 you should be able to upgrade to Java 5 or 6 without any problem.

As Campbell says, you also have to set the compiler compliance level in Eclipse to the right Java version.
 
reply
    Bookmark Topic Watch Topic
  • New Topic