Jump to content
SubSpace Forum Network

Recommended Posts

Posted

I know there's an extremely simple answer to my question, and i'll probably kick myself once i figure it out.

 

I started looking into java again with xog's posting, and im trying to replicate his lotto program just as a warmup.

 

However, I CANNOT FOR THE LIFE OF ME, figure out what the most simple input request is.

 

I know its like this somehow:

public static void main(String[] args)
{
    ....
    System.out.print("Please type something here: ");
//I CANNOT FIGURE OUT WHAT GOES HERE. I THOUGHT IT WAS:
    String str = System.in.getString(); //or something along the lines of that. but i can't remember the exact syntax.
    ....
}

i am going to kick myself once i get the answer because i know it is so simple....

and yes, i've tried google for the last 30min... i dont want to use JOptionPane or any other GUI.

Posted
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
   public static void main(String[] args) {
       InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader input = new BufferedReader(isr);
       String inStr;
       System.out.println("Enter a string:");
       try {
         inStr = input.readLine();
         System.out.print("You entered ");
         System.out.println(inStr);
       }
       catch (IOException e) {
           System.out.println("Error");
       }
   }
}

Posted (edited)

i specifically remember a simple 1 line type of deal, that didn't require InputStream.

import java.util.Scanner;

public class InputExp {

  public static void main(String[] args) {

      String name;
      int age;
      Scanner in = new Scanner(System.in);

      // Reads a single line from the console 
      // and stores into name variable
      name = in.nextLine();

      // Reads a integer from the console
      // and stores into age variable
      age=in.nextInt();
      in.close();            

      // Prints name and age to the console
      System.out.println("Name :"+name);
      System.out.println("Age :"+age);

   }
}

Something like that?

Source: http://www.java-tips.org/java-se-tips/java.util/how-to-read-input-from-console.html

 

 

Edit: I haven't really used scanner a lot. I normally use something like:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadConsoleSystem {
 public static void main(String[] args) {

System.out.println("Enter something here : ");

try{
    BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
    String s = bufferRead.readLine();

    System.out.println(s);
}
catch(IOException e)
{
	e.printStackTrace();
}

 }
}

Edited by BDwinsAlt
Posted

I think

...
String var = System.in.nextline();
...

Was what it was. Ill try it when I get home and let you know how it works...

I did think I have to import java.util.* but I don't remeber ever calling an instance of a Scanner object.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...