Be a Supporter!

Netbeans Help - Java Program

  • 549 Views
  • 9 Replies
New Topic Respond to this Topic
UnknownFear
UnknownFear
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 37
Gamer
Netbeans Help - Java Program 2009-10-26 16:18:44 Reply

I 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.

UnknownFear
UnknownFear
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 37
Gamer
Response to Netbeans Help - Java Program 2009-10-26 18:15:45 Reply

Can anyone give me some help with this? Please?

CronoMan
CronoMan
  • Member since: Jul. 19, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to Netbeans Help - Java Program 2009-10-27 04:30:30 Reply

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"

UnknownFear
UnknownFear
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 37
Gamer
Response to Netbeans Help - Java Program 2009-10-29 16:19:17 Reply

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.

kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Netbeans Help - Java Program 2009-10-29 16:41:48 Reply

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());}
fourthfrench
fourthfrench
  • Member since: Aug. 13, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Netbeans Help - Java Program 2009-10-29 17:06:20 Reply

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

UnknownFear
UnknownFear
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 37
Gamer
Response to Netbeans Help - Java Program 2009-10-30 10:04:10 Reply

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.

Palias
Palias
  • Member since: Oct. 27, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Netbeans Help - Java Program 2009-10-30 10:14:07 Reply

I use Eclipse for programming Java.
Very nice indeed ;D

UnknownFear
UnknownFear
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 37
Gamer
Response to Netbeans Help - Java Program 2009-10-30 11:03:18 Reply

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.

Palias
Palias
  • Member since: Oct. 27, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Netbeans Help - Java Program 2009-10-30 11:57:39 Reply

At 10/30/09 11:03 AM, UnknownFear wrote:
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.

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