? howto input data with default constructor then output to single string

I need to input user data with a default constructor then output all that data into a single string with a method.

Code Below:

import java.io.*; //Package for stream readers

public class GradeSheet {

public sheet data(){

//Defines Strings to hold user data

String last_initial;

String first_name;

String student_ID_num;

String course_name;

String course_num;

String asg_name;

String asg_num;

}

public static void main(String args[]) throws IOException

{

//Clears the display device

for (int n = 0; n < 80; n++) System.out.println();

// Input stream for strings

BufferedReader in;

in = new BufferedReader(new InputStreamReader(System.in));

//Determine weather Default Constructor has been activated to handle Data Input

//Ask on Java Forum

System.out.print("Please enter the Initia of your last name: ");

last_initial = in.readLine();

System.out.print("Please enter your First Name: ");

first_name = in.readLine();

System.out.print("Please enter your Student ID#: ");

student_ID_num = in.readLine();

System.out.print("Please enter your current Course Name: ");

course_name = in.readLine();

System.out.print("Please enter your current Course Number: ");

course_num in.readLine();

System.out.print("Please enter your current Assignment Name: ");

asg_name = in.readLine();

System.out.print("Please enter your current Assignment Number: ");

asg_num = in.readLine();

//Convert inputs into a single string

//Add printGradeSheet method to print string

System.out.println("\n" + course_num + " " + course_name + "\n" + asg_name + " " + asg_num + "\n" + first_name + " " + last_initial + ".");

}

}

Note: I also need to input only certain data into strings how do I input data without using strings?

Thanks

[1973 byte] By [cyelineka] at [2007-11-27 9:22:59]
# 1

> I need to input user data with a default constructor

> then output all that data into a single string with a

> method.

Ok, then provide a constructor in your GradeSheet class.

Details: http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html

> Code Below:

>

> ...

When posting code, please use code tags:

http://forum.java.sun.com/help.jspa?sec=formatting

> Note: I also need to input only certain data into

> strings how do I input data without using strings?

>

> Thanks

User input from the console is always a String. After you get the String, you could convert it into something else, like an int or double, etc.

See the Integer class how to convert (or parse) a String into an int:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html

prometheuzza at 2007-7-12 22:17:47 > top of Java-index,Java Essentials,New To Java...
# 2

You didn't say, but you're probably getting compiler error messages because your (static) main method can't put data into instance fields. Also, because "public sheet data()" makes no sense and is syntactically invalid.

There are a variety of ways to handle this, but in my opinion the cleanest way is:

1) create a different class to provide the UI to get user data, and put the main() method in there. Putting the UI in the GradeSheet class is ugly.

2) In your main method, create local variables to hold the various fields that you're asking for. (This is basically what you have in "sheet data()".)

3) when you get all the fields you need, then create a GradeSheet object, with multiple arguments, passing as parameters the local variables that you created in (2).

In other words, do NOT use the no-arg constructor.

If you absolutely must use a no-arg constructor, because your prof is an idiot...then, you have a couple choices. One is to have a no-arg constructor, then have setter methods for the various fields. The other is to basically put what is in your main method, into the no-arg constructor. God that's ugly though...

Either way, you need fields in GradeSheet. You have local variables in "sheet data()", which maybe is confusing you.

When you post code, please wrap it in [code][/code] tags so it's easier to read.

> Note: I also need to input only certain data into

> strings how do I input data without using strings?

User input is almost always going to be strings. If you need data of some other type, then parse it. There are methods like Integer.parseInt that can help you with that.

paulcwa at 2007-7-12 22:17:47 > top of Java-index,Java Essentials,New To Java...