-
System.in.read (newbie)
i was reading up on this syntax and it said that this automatically stores everything as int and ot convert it to char you would need to do something like - ch = (char) System.in.read();. well i was wondering if there is a way to convert to and store a string. i am basically looking to store a string value (name) through user input.
-
 Originally Posted by someone
i was reading up on this syntax and it said that this automatically stores everything as int and ot convert it to char you would need to do something like - ch = (char) System.in.read();. well i was wondering if there is a way to convert to and store a string. i am basically looking to store a string value (name) through user input.
Then don't use System.in.read but InputStreamReader and BufferedReader. Here's an example:
Code:
import java.io.*;
public class GetName {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = "";
// continue when something is entered.
while(name.equals("")) {
System.out.print("Hi there, enter your name: ");
try {
name = br.readLine();
}
catch(Exception e) { e.printStackTrace(); }
}
// print the name
System.out.println("\nWelcome "+name+"!\n");
}
}
Good luck.
-
this is the code we were given to read input from the command prompt window. it'll take the whole thing as a string until the enter key is pushed.
Code:
import java.io.*;
public class Keyboard {
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static String readInput() {
try {
return in.readLine();
}
catch (IOException e) {
System.out.println("An error has occurred in the Keyboard.readInput() method");
System.out.println(e.toString());
System.exit(-1);
}
return null;
}
}
Basically it creates a buffered reader using System.in as the argument. then it'll read a line of text as return it.
and if an IO exception is caught then you'll know about it.
I prefer to live in my own shadow. At least then I can be compared to a false past I'm more familiar with
-
Tiger lovin':
Code:
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String input = in.next();
System.out.println("Hello, " + input);
Similar Threads
-
By Itai Raz in forum Open Source
Replies: 2
Last Post: 09-01-2003, 05:22 PM
-
By Benno in forum Careers
Replies: 10
Last Post: 02-10-2002, 07:07 PM
-
By Mark Burns in forum .NET
Replies: 164
Last Post: 03-13-2001, 12:43 PM
-
By Paul in forum Enterprise
Replies: 1
Last Post: 12-07-2000, 06:11 PM
-
By Anthony Saffer in forum Java
Replies: 1
Last Post: 09-22-2000, 11:33 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|