The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 ViewsI am testing out the NetBeans IDE for Java and other languages, and it is going quite well. Now, for the program, I am asking the user to enter their name, but I can't figure out how to initialize the variable. Here is the code.
public class Main {
//Variables
String word = "";
public void getName()
{//Ask user for their name
System.out.print("Please enter your name: ");
String name = name;
}//getName
public void displayName()
{//Display user's name
System.out.println("Hello, " + name);
}//displayName
public static void main(String[]args)
{//main
Main program = new Main();
program.getName();
program.displayName();
}
}
I just need to figure out how to input information into Java. I used to use blueJ and I did it via the "In" class, but NetBeans doesn't have it.
Can anyone give me some help with this? Please?
At 10/26/09 06:15 PM, UnknownFear wrote: Can anyone give me some help with this? Please?
System.in.readln()
"no sound in ass"
At 10/27/09 04:30 AM, CronoMan wrote:At 10/26/09 06:15 PM, UnknownFear wrote: Can anyone give me some help with this? Please?System.in.readln()
No, I don't need help with that, the correct syntax is System.out.print. I just need help with being able to input data into it so I can store it for later use.
At 10/27/09 04:30 AM, CronoMan wrote:At 10/26/09 06:15 PM, UnknownFear wrote: Can anyone give me some help with this? Please?System.in.readln()
There is no System.in.readln() in java
The easiest way to get input from the console is to use a Scanner class available in java.util
Scanner sc = new Scanner(System.in);
System.out.print(sc.next());} Your code is very convoluted.
Below is what you actually want.
import java.util.Scanner;
public class Main {
static String name = "";
static Scanner sc = new Scanner(System.in);
public static void getName() {
System.out.print("Please enter your name: ");
name = sc.nextLine();
}
public static void displayName() {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
getName();
displayName();
}
}
~fourthfrench
At 10/29/09 05:06 PM, fourthfrench wrote: Your code is very convoluted.
Below is what you actually want.
Wow. I am definitely not being taught that in school at all. We're using BlueJ and a program to get and display information is very simplified. I don't see why we aren't being taught Java that way, though. Very confusing.
I use Eclipse for programming Java.
Very nice indeed ;D
At 10/30/09 10:14 AM, Palias wrote: I use Eclipse for programming Java.
Very nice indeed ;D
I was going to go with Eclipse, but than I saw that NetBeans has support for other languages, so I chose that program.
But I still don't understand the BufferRead thing or Scanner or anything. For some unknown reason, I was never taught that my computer course and I've been taking it since grade 10. Very disappointing as now I feel like I learned Java the wrong way and none of it works.
At 10/30/09 11:03 AM, UnknownFear wrote:At 10/30/09 10:14 AM, Palias wrote: I use Eclipse for programming Java.I was going to go with Eclipse, but than I saw that NetBeans has support for other languages, so I chose that program.
Very nice indeed ;D
But I still don't understand the BufferRead thing or Scanner or anything. For some unknown reason, I was never taught that my computer course and I've been taking it since grade 10. Very disappointing as now I feel like I learned Java the wrong way and none of it works.
There are some good tutorials here http://java.sun.com/docs/books/tutorial/
where you can pretty much learn java and OO from scratch.
Also, there is a complete reference for all the classes and stuff here: http://java.sun.com/javase/6/docs/api/in dex.html?overview-summary.html
which is incredibly useful
And for the Buffered Reader, you need to learn about input and output streams in java.
I recomend reading this if you are having problems with I/O.
http://java.sun.com/docs/books/tutorial/
essential/io/buffers.html