11 Replies - 513 Views - Last Post: 11 May 2011 - 01:38 PM Rate Topic: -----

#1 sarah waleed  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 124
  • Joined: 12-January 11

i have a problem in reading from the keyboard

Posted 11 May 2011 - 12:07 PM

hello everybody,
i start with that to do my work, but i have a problem in reading from the (kb),

import java.io.*;
class sack{
public static void main(String[] args)  {
System.out.println("\n enter your string \n ");
InputStreamReader io= new InputStreamReader(System.in);
 BufferedReader br = new BufferedReader(io);
 System.out.println("io"+br);        
}
}





when it run it will not let me inter any thing
it just type all that
enter your string
iojava.io.BufferedReader@190d11

and said process complete
what is the problem please help
thanks in advance

This post has been edited by sarah waleed: 11 May 2011 - 12:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: i have a problem in reading from the keyboard

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 12:13 PM

Why dont you use the Scanner class to read from the keyboard! and what exactly you want to read. In your code you missed .readLine() methode types and also a variable to store what you have read!:

This post has been edited by smohd: 11 May 2011 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 sarah waleed  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 124
  • Joined: 12-January 11

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 12:17 PM

thank u for reply
i want to read an ip from keybord
to compare it with a hashmap that i built earliar i also try with scanner it also not work i think i use it in a wromg way
thanks for help
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 12:23 PM

First you must have a variable to store what you are going to read! like this:
String str = new BufferedReader(new InputStreamReader(System.in)).readLine();

Was This Post Helpful? 1
  • +
  • -

#5 sarah waleed  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 124
  • Joined: 12-January 11

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 12:30 PM

View Postsmohd, on 11 May 2011 - 07:23 PM, said:

First you must have a variable to store what you are going to read! like this:
String str = new BufferedReader(new InputStreamReader(System.in)).readLine();

do u mean using this with scaner?
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 12:43 PM

That is a buffer reader, but the scanner is the same you must have a variable to store what you are going to read.
If you want to use Scanner class then it will import java.util.Scanner, then :
Scanner keyboard = new Scanner(System.in);
String str = keyboard.nextLine();

Was This Post Helpful? 1
  • +
  • -

#7 sarah waleed  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 124
  • Joined: 12-January 11

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 12:50 PM

thanks for help
i want the user to enter an ip address then i take that ip as a value to a hashmap that i made before(the value and the key of the map are string), then i return to hin the key of that ip address, then he inter that two(ip address and the key) if it match he win else he loss
so how can i read the first enter(ip add), then the two enter (ip an correct key)?
thanks for time u take to read that
with best regards
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 12:58 PM

Just use any of them either Scanner class or bufferReader. First you will create an instance of one of them like how I did it before then you will ask the user to enter as you planned and check for equality> Try it your self, and if you get problem you can show us what you have tried and where you failed. This site plans to build strong programmer who can try things that they think complex!
Was This Post Helpful? 1
  • +
  • -

#9 sarah waleed  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 124
  • Joined: 12-January 11

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 01:09 PM

:^: thank u for all the time u spend to help me i m so glad i can enter any thing from the kb,
by this
import java.io.*;
import java.util.*;
public class synack{
public static void main(String[] args)  {
System.out.println(" please inter your correct ip: ");
Scanner keyboard = new Scanner(System.in);  
String str = keyboard.nextLine(); 
System.out.println("inter two keys");
  Scanner ip = new Scanner(System.in);  
String str1 = ip.nextLine();
String str2 = ip.nextLine();
}
}




soooooo much thank
i can now start doing my work
thanksss again
am sorry that i can't press the bottun more than 1
best regards
:)

what i inter is now in (str,str1 & str2)
is that right?

This post has been edited by sarah waleed: 11 May 2011 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 01:21 PM

Glad I help. But also you may need to know other methods available in Scanner class, look at the method summary and other issues concerned here.

Also you dont need to create a new object for every input, you can just say:
public static void main(String[] args)  {
Scanner keyboard = new Scanner(System.in); 

System.out.println(" please inter your correct ip: ");
String str = keyboard.nextLine(); 
System.out.println("inter two keys");
  //Scanner ip = new Scanner(System.in); no need  
String str1 = keyboard.nextLine();
String str2 = keyboard.nextLine();
}

This post has been edited by smohd: 11 May 2011 - 01:28 PM

Was This Post Helpful? 1
  • +
  • -

#11 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,172
  • Joined: 05-April 11

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 01:23 PM

View Postsarah waleed, on 11 May 2011 - 01:09 PM, said:

:^: thank u for all the time u spend to help me i m so glad i can enter any thing from the kb,
by this
import java.io.*;
import java.util.*;
public class synack{
public static void main(String[] args)  {
System.out.println(" please inter your correct ip: ");
Scanner keyboard = new Scanner(System.in);  
String str = keyboard.nextLine(); 
System.out.println("inter two keys");
  Scanner ip = new Scanner(System.in);  
String str1 = ip.nextLine();
String str2 = ip.nextLine();
}
}




soooooo much thank
i can now start doing my work
thanksss again
am sorry that i can't press the bottun more than 1
best regards
:)

what i inter is now in (str,str1 & str2)
is that right?


You shouldn't use more than one scanner. Also I think you should be using BufferedReader for this task.

It is bad practice to make more than one Scanner object for the same file, because you can get a problem with the File being blocked (unable to be read) because the other Scanner is already reading it. You won't have this problem because you are using System.in (I think, not 100% sure). At least that's how it works with BufferedReader if you don't close it.
Was This Post Helpful? 1
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: i have a problem in reading from the keyboard

Posted 11 May 2011 - 01:38 PM

That is a good idea for your work. You may read buffer reader from the links below:
Class BufferedReader Documentation
InputStreamReader Documentation
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1