Actually i try to create a method which can call the scanner keyboard
firstly i was trying this

void makeRequest(){
request ="Hello";
}
So i wanna make like
when i call this method it will tell me to input the String by scanning the keyboard
is it possible to do that?
and how to do that plz show it to me i'm kind of stuck at this method

Recommended Answers

All 12 Replies

Actually i try to create a method which can call the scanner keyboard
firstly i was trying this

void makeRequest(){
request ="Hello";
}
So i wanna make like
when i call this method it will tell me to input the String by scanning the keyboard
is it possible to do that?
and how to do that plz show it to me i'm kind of stuck at this method

should it be...readLineMethod() ???

Declare a temporary String variable to get the input, and invoke the readLine()method to get input from the keyboard.

plz guie me more

but make sure you have an instance of the Scanner class on which you can run the methods of the Scanner class.

to see how it works: look here

you must also ask yourself: will you only use this value in this method, or do you need this value in the method that calls this method.

In that case, I would change the return type from void to String, and return the value you've entered using the Scanner class.

Hi Guys,
At school, we just started programming in Java/ BlueJ
After adding a BufferedReader one of my attributes is always 0.0 - but i don't find my mistake. pls Help me
ps. sry for my bad english

import java.io.*;
    public class good_machine_v2 {                                                                 

private double sum;                                                                     
private double cola;                                                                            
private double fanta;                                                                           
private double water;                                                                              
private double sprite;                                                                          
private double price;                                                                           
private double price2;                                                                             
private double wert;                                                                            
private boolean moredrinks;
private boolean check1;
private boolean check2;  
private boolean check3;                                                               


    public good_machine_v2 ()                                                                      
        {
           gesamtsumme = 0.0;
           preis = 0;
           cola= 1.5;
           fanta=1.0;
           wasser=1.0;
           sprite=1.5;
        }
       public void select_drink () throws IOException                                                              

    {
        System.out.print("Please select drink (String) : ");
        BufferedReader cola = new BufferedReader(new InputStreamReader(System.in));
        String information3 = cola.readLine();

            if (information3 == "cola")
           {
            price = 1.5;
           }
          else {
            if ( information3 == "fanta")
            {
             price = 1.5;
            }
            else {
                if (information3=="water")
                {
                    price =1.0;
                }
                else 
                { 
                    if (information3 == "sprite")
                    {
                        price= 1.5;
            }
            else {}
        }
            }
           }
          System.out.println("You selected "+  information3 + " . It costs :" + price );

}

Hi Guys,
At school, we just started programming in Java/ BlueJ
After adding a BufferedReader one of my attributes is always 0.0 - but i don't find my mistake. pls Help me
ps. sry for my bad english

don't hijack someone else's thread, especially not if the issues are not related!
we are here trying to solve the OP's problem. if you want us to take a look at your code:

  1. start a new thread
  2. explain specific what problem you are having
  3. add your code there between Code tags, so they'll be more readable

alright i think my code is working now
i just simply took "request = input.nextLine()"
so when i call it it works
but i'm not sure it's right or wrong way to do it

Sry about that. I'm new at this Webpage.
I found my mistake myselve ))

that would depend on what input is.
can you ellaborate a bit more on the code that you have now?

alright i think my code is working now
i just simply took "request = input.nextLine()"
so when i call it it works
but i'm not sure it's right or wrong way to do it

Scanner input = new Scanner(System.in);
String request = input.nextLine(); // reads the entire String you enter
request = input.next(); // reads the first word of the String you enter
import java.net.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;


public class UDP {

	
	final int buffSize = 10001;//buff size
        final int TIMEOUT = 3000;
        final int MAXTIRES = 1;
        boolean receivedResponse = false;
	DatagramSocket sock;
	String request;
	String response;
	InetAddress servAddr;
	int servPort ;
         Scanner input = new Scanner(System.in);
        
	
UDP(DatagramSocket s, String sName,int sPort) throws UnknownHostException
	
	{
		sock = s;
		servAddr = InetAddress.getByName(sName);
		servPort=sPort;
	}

String makeRequest(){
	
	request = input.nextLine();//code
        
        return request;
}
void sendRequest() throws IOException
{
	try
	{
		
		byte[] sendBuff = new byte [buffSize];
		sendBuff = request.getBytes();
		DatagramPacket sendPacket = new DatagramPacket(sendBuff,sendBuff.length,servAddr,servPort);
		sock.send(sendPacket);
                
	
	}
	catch (SocketException ex)
	{
		System.err.println("Exception in getRequest");
		ex.printStackTrace();
	
	}
	
}
	void getResponse()  throws IOException{
		try{
			byte[] recvBuff = new byte[buffSize];
			DatagramPacket recvPacket = new DatagramPacket (recvBuff,buffSize);
			sock.receive(recvPacket);
			recvBuff = recvPacket.getData();
			response = new String(recvBuff,0,recvBuff.length);
                        sock.setSoTimeout(TIMEOUT);
                        
                        
                            
                        
                        
              {
		
              
              }
	    
                        
                        
		}
                        
		catch (SocketException ex)
		{
                    ex.printStackTrace();
                    System.err.println("Response Exception");
			
		}
                
       
                
	
        }
        
       
	
	void useResponse(){
		
		System.out.println(response);//code for response
	}
  
        
     
	
	void close ()
	{

	sock.close();
	}
	
        
        
        public static void main(String args []) throws IOException
	{
       int servPort = 0;//add port
       String servName = "127.0.0.1" ; //add servername
       
      Scanner input = new Scanner(System.in);
       
         System.out.println("in put port");
         servPort = input.nextInt();
	
	DatagramSocket sock = new DatagramSocket ();
	UDP client = new UDP (sock, servName,servPort);
    
       
       
       //servPort= input.nextInt();
      // System.out.println(servPort);
       //System.out.println("in put name");
       //servName=input.nextLine();
       // System.out.println(servPort);
       // System.out.println();
        
	
	System.out.println("Enter message");
        client.makeRequest();
	client.sendRequest();
	client.getResponse();
	client.useResponse();
	client.close();
	
	
	
	}
	
}

This is my code
i'm like want to send the input String by calling makeRequest()

looks to me like it should work.
does it?

Yes it does thank you a lot anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.