Results 1 to 19 of 19
Thread: [SOLVED] Making a list in java.
- 02-15-2009, 06:01 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
[SOLVED] Making a list in java.
Hi,
I'm compleately new to java and any other programming language aswell.
I need to make a simple program that allows the user to enter six names before the program stops taking any more input and lists the names separated by commas.
I'm having trouble figuring out how to accomplish this, and would appreciate any input that would point me in the right direction.
Thanks
- 02-15-2009, 06:10 AM #2
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
ok.. so what your gonna do is set 6 blank variables all with different names. then your gonna ask the user for 6 names and set each name to a different variable. then print out each variable seperated by commas..
this sounds like HW so i wont give you the code.
good luck hope i could help
-
think for-loop and an array of String.
- 02-15-2009, 06:21 AM #4
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
Oh, where is MK12 with tutorials and range of codes )
VinTiger, and you talk about GUI or console app?
- 02-15-2009, 06:30 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Thanks,
So, do I use the variable "char" to store the names in?
and if so, how do I make it a blank variable that accepts the users input?
thanks again
-
VinTiger, I'm not meaning to insult you, but you are asking some extremely basic questions that suggest that you may not be keeping up in class.
I think you need to either re-read your notes, book or the very basic tutorials. You don't know enough for us to be able to help you yet, but with study you can change this. Good luck.
- 02-15-2009, 07:00 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
ill give you a hint..
if i type one letter its a character.
char bob="a";
you need a string it can hold long multiple chracters like a name.
string bob='bob';
- 02-15-2009, 07:01 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
also.. if you need a blank variable just dont give it a value.
string bob;
its simple.
- 02-15-2009, 07:14 AM #9
I would have to agree with Fubarable... you need to know more java programing basics before you continue. To answer your questions:
No, char is a variable type for characters, example:So, do I use the variable "char" to store the names in?
Java Code:char letter = 'a';
to create a variable (a string) for the user's input you would do the following:how do I make it a blank variable that accepts the users input?
String name = "";
Please review the following link... it contains everything you need to know about starting to program in Java:
The Java™ Tutorials
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-15-2009, 07:18 AM #10
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
i think what he is asking is VERY simple. he shouldt have to much trouble if he just keeps thinking about it.
ask for 6 names.
set the names as 6 variables.
print the 6 names.
- 02-15-2009, 06:57 PM #11
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Thanks Ramsrocker, that's exactly what I needed to know. I know that these questions must seem trivial to someone that knows programming, but right now I'm still trying to to get my head around where all these variables and statements belong in the code.
Thanks again
- 02-15-2009, 07:42 PM #12
I'll give a little pseudo code:
Hope this helped.Java Code:import the Scanner class (search google "Java Scanner api 6") Your Class { TheMethodThatDoesTheWork() { Declare a String; Make a new instance of Scanner; loop 6 times { StringVariable = Scanner's method that gets the next word /* check out the Scanner class's api */ Print out: StringVariable followed by a comma } Close the Scanner; } main method { call your method. If non-static, make an instance of your class to call it. } }Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-15-2009, 10:39 PM #13
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Thanks, it did help a lot.
Unfortunately I'm still doing something wrong. I'm posting my begginners code for anyone to look at. I'm geting a error message saying: illegal start of expression.
Thanks again
import java.util.Scanner;
import java.io.*;
public class NamesInput5
{
public static void main(String[] args)
{
// Declarations
String names;
int i;
int begin = 0;
int end = 5;
Scanner in = new Scanner(System.in);
// Prompts
for(i = begin; i <= end; i++) {
System.out.println("Enter name");
// Read in values
names = in.nextLine();
}
System.out.println("Here is what you entered: ");
System.out.println(names,);
}
}
- 02-15-2009, 10:46 PM #14
use code tags please. Heres problem:
get rid of the commaJava Code:System.out.println(names[COLOR="Red"],[/COLOR]);
It still won't work, it will only print the last name entered. try this:
Better yet, instead of printing the names variable, just get rid of the names variable (and its declaration) and do this instead:Java Code:#13 (permalink) Today, 04:39 PM VinTiger Member Join Date: Feb 2009 Posts: 4 Thanks, it did help a lot. Unfortunately I'm still doing something wrong. I'm posting my begginners code for anyone to look at. I'm geting a error message saying: illegal start of expression. Thanks again import java.util.Scanner; import java.io.*; public class NamesInput5 { public static void main(String[] args) { // Declarations String names; int i; int begin = 0; int end = 5; Scanner in = new Scanner(System.in); // Prompts [COLOR="DarkOrange"]// Put the orange line here[/COLOR] for(i = begin; i <= end; i++) { System.out.println("Enter name"); // Read in values names = in.nextLine(); [COLOR="Blue"]// put blue line here[/COLOR] } [COLOR="DarkOrange"]System.out.println("Here is what you entered: ");[/COLOR] [COLOR="Blue"]System.out.println(names[COLOR="Red"],[/COLOR]);[/COLOR] } }
Some other tips:Java Code:System.out.print(in.nextLine());
- use CODE tags
- the begin and end are pointless waste of space, just use the numbers in your loop, don't use the variables, e.g. for(i = 0; i <= 5; i++)
-Instead of i <= 5 (when you change end to 5), do < 6. same meaning, but more commonly done this way
- dont skip so many lines in between statements.
- Try putting all that in a method and then call the method in the main method.
Hope this Helped
-MK12Last edited by MK12; 02-15-2009 at 10:57 PM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-15-2009, 10:58 PM #15
Other problems witht your program
Minor: you might get a nastygram indicating that you haven't initiated a variable... just make variable names = ""; to make that msg go away.
Major: You're not saving all the names that you entering... only the last (you're overwirting all the names since you're using only one variable: names). To avoid this, save the names (strings) that are entered to an array. Here's a link to what an array is and how to use it:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-15-2009, 11:00 PM #16
Ohh yeah I forgot my way would print what they said immediately after..
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-15-2009, 11:20 PM #17
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Thanks for all the help guys,
I didn't realize just how much were involved in learning to program, but I'll keep trying until I get it right.
Thanks again
- 02-16-2009, 12:29 AM #18
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Ok, I got my program to work. The arrays were what I needed to learn in order to save my input and then present it after the loop had finnished.
Thanks a lot:)
- 02-16-2009, 12:41 AM #19
Similar Threads
-
Need help with Java classes for making a program.
By TheDarkReverend in forum Advanced JavaReplies: 2Last Post: 11-28-2008, 04:50 AM -
Making Java Application Executable
By jadaleus in forum Advanced JavaReplies: 4Last Post: 10-23-2008, 12:59 PM -
making a count down timer using java
By saytri in forum New To JavaReplies: 3Last Post: 12-29-2007, 09:49 PM -
Making connect 4 in java
By zoe in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:10 AM -
Making a Java web calendar
By toby in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 08-07-2007, 06:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks