Forum Topic: Netbeans Help - Java Program

(213 views • 9 replies)

This topic is 1 page long.

<< < > >>
Questioning

UnknownFear

Reply To Post Reply & Quote

Posted at: 10/26/09 04:18 PM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,911

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.

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


Misunderstood

UnknownFear

Reply To Post Reply & Quote

Posted at: 10/26/09 06:15 PM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,911

Can anyone give me some help with this? Please?

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


None

CronoMan

Reply To Post Reply & Quote

Posted at: 10/27/09 04:30 AM

CronoMan EVIL LEVEL 06

Sign-Up: 07/19/04

Posts: 2,987

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"


None

UnknownFear

Reply To Post Reply & Quote

Posted at: 10/29/09 04:19 PM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,911

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.

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


None

kiwi-kiwi

Reply To Post Reply & Quote

Posted at: 10/29/09 04:41 PM

kiwi-kiwi LIGHT LEVEL 08

Sign-Up: 03/06/09

Posts: 650

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());}

None

fourthfrench

Reply To Post Reply & Quote

Posted at: 10/29/09 05:06 PM

fourthfrench LIGHT LEVEL 05

Sign-Up: 08/13/09

Posts: 17

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


Questioning

UnknownFear

Reply To Post Reply & Quote

Posted at: 10/30/09 10:04 AM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,911

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.

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


None

Palias

Reply To Post Reply & Quote

Posted at: 10/30/09 10:14 AM

Palias NEUTRAL LEVEL 02

Sign-Up: 10/27/09

Posts: 4

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


None

UnknownFear

Reply To Post Reply & Quote

Posted at: 10/30/09 11:03 AM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,911

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.

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


None

Palias

Reply To Post Reply & Quote

Posted at: 10/30/09 11:57 AM

Palias NEUTRAL LEVEL 02

Sign-Up: 10/27/09

Posts: 4

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


All times are Eastern Standard Time (GMT -5) | Current Time: 04:57 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!