Java Programming

what is the input statement in java

Page 1 of 1

10 Replies - 2263 Views - Last Post: 04 August 2010 - 04:56 PM Rate Topic: -----

#1 joshua112x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-August 10

Java Programming

Posted 04 August 2010 - 05:30 AM

hi guys can you help me about this one im in 1st year in bs I.T im a little bit confused in some java statement.... please help me with this one " what is the input statement in java Programming?"

Attached image(s)

  • Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Java Programming

#2 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Java Programming

Posted 04 August 2010 - 05:40 AM

View Postjoshua112x, on 04 August 2010 - 11:30 AM, said:

hi guys can you help me about this one im in 1st year in bs I.T im a little bit confused in some java statement.... please help me with this one " what is the input statement in java Programming?"


Your question is not clear. :ph34r:

Are you asking how to get user input in java?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Java Programming

Posted 04 August 2010 - 06:07 AM

The common methods of user input for beginners are Scanner and JOptionPane.
Was This Post Helpful? 0
  • +
  • -

#4 adhish94  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • Posts: 45
  • Joined: 12-July 10

Re: Java Programming

Posted 04 August 2010 - 08:42 AM


import java.io.* // imports all classes in the pre-defined input-output package
class name
{
public static void main()throws IOException // for catching a run-time error; you can also use a try-catch block
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter string");
String st = br.readLine(); // reads the input typed; automatically reads as String
System.out.println("Enter a number");
int num = Integer.parseInt(br.readLine()); // reads the input typed and converts it to int type
// write the rest of the code here
}
}




If you are using something like BlueJ, you can also input stuff as:


public static void main(int n, String s)




You can also use the scanner class.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Java Programming

Posted 04 August 2010 - 08:48 AM

BlueJ may do something in the background with main(), but it is really horrid Java practice to overload main() regardless of IDE. Also, main() should accept a String[], not no-params as you try to here: public static void main()throws IOException.

In addition, please properly indent your code. :)
Was This Post Helpful? 0
  • +
  • -

#6 adhish94  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • Posts: 45
  • Joined: 12-July 10

Re: Java Programming

Posted 04 August 2010 - 08:54 AM

Okay, by the way, I parameterized main only when I had just begun programming. Anyway, thanks for the advice.
Was This Post Helpful? 0
  • +
  • -

#7 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Java Programming

Posted 04 August 2010 - 11:46 AM

OP, can you make your question clearer please? If I'm not mistaken, you want to know the ways to get input from users? You can use Scanner class or JOptionPane. Scanner class is the command-line way of getting input from user, while JOptionPane is the GUI way. This is an example on how to use both of them.

import java.util.Scanner;

public class testing{

     public static void main(String[] args){
          
          //Declaring a scanner variable
          Scanner scanner = new Scanner(System.in);

          //Assuming the input is a string... 
          String str = scanner.nextLine();

          //Printing out what the user entered
          System.out.println(str);

     }

}



This is the JOptionPane way.

import javax.swing.JOptionPane;

public class testing{

     public static void main(String[] args){

          //Getting input from user by using JOptionPane
          //and assigning the data to "str"
          String str = JOptionPane.showInputDialog(null, "Enter the data: ");
          System.out.println(str);
 
     }

}



EDIT: One question...what does your attached thumbnail have to do with your question? I don't get it.

This post has been edited by Cuzzie: 04 August 2010 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Java Programming

Posted 04 August 2010 - 12:20 PM

I am curious why hasn't anyone said anything about BufferedReader is there some advantage to using Scanner over BufferedReader?

Just because I mentioned it I will give you an example of the code to get input from the user
 System.out.println("Please Enter Something");
            BufferedReader readIn = new BufferedReader(new InputStreamReader(System.in));
            String str = null;
                    try{
           str = readIn.readLine();
            }catch(IOException e)
                    {
            e.printStackTrace();
                        System.out.println("Error: IOException!");
            }
            System.out.println("Input Message: "+ str);
        }


This post has been edited by b0ng01: 04 August 2010 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Java Programming

Posted 04 August 2010 - 12:24 PM

Scanner is more newbie friendly, but has a couple glitches with next() and nextLine(). It also allows for reading in tokens as primitives, BigDecimals, BigIntegers, pattern matching, etc. It has a lot of built in functionality that BufferedReader doesn't. BUT a lot of developers beyond the newbie stage prefer parsing the Strings themselves, so use BufferedReader.
Was This Post Helpful? 0
  • +
  • -

#10 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Java Programming

Posted 04 August 2010 - 12:27 PM

View Postb0ng01, on 04 August 2010 - 06:20 PM, said:

I am curious why hasn't anyone said anything about BufferedReader is there some advantage to using Scanner over BufferedReader?

Just because I mentioned it I will give you an example of the code to get input from the user
 System.out.println("Please Enter Something");
            BufferedReader readIn = new BufferedReader(new InputStreamReader(System.in));
            String str = null;
                    try{
           str = readIn.readLine();
            }catch(IOException e)
                    {
            e.printStackTrace();
                        System.out.println("Error: IOException!");
            }
            System.out.println("Input Message: "+ str);
        }



Most newbies use Scanner at the beginning, and they move on to using BufferedReader when they have a better understanding on the basics of Java. That's what my lecturer said. :P
Was This Post Helpful? 0
  • +
  • -

#11 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Java Programming

Posted 04 August 2010 - 04:56 PM

Perhaps the most common method of input for Java is the scanner method.

import java.util.Scanner;

public class Scanner
{
   public static void main(String[] args)
   {
      System.out.print ("Please enter your first name:\t");
      Scanner scan = new Scanner (System.in);
      input = scan.nextLine();

      System.out.println ("\nYour first name is " + input + ".");
   }
}


This post has been edited by thughes2009: 04 August 2010 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1