User input in Java

deadlyapp

Diamond Member
Apr 25, 2004
6,656
737
126
Easy, I know, but I don't have any programming experience so to speak of, and I've been thrown into a java class...

Could someone tell me the command for a user input? The program prompts different things then places them into a poem. I already have it written, but when I run it it doesn't prompt, rather it just places null because the user never inputted...

Code looks like this:

public class Poem1 {
public static void main(String[] args) {
Scanner scr = new Scanner(System.in) ;
System.out.println ("What is your name?") ;
String name = scr.nextLine() ;
System.out.println ("What is something red?") ;
String red = scr.nextLine() ;
System.out.println ("What is something blue?") ;
String blue = scr.nextLine() ;
System.out.println ("What is something sweet?") ;
String sweet = scr.nextLine() ;
System.out.println ("What else is sweet?") ;
String sweet2 = scr.nextLine() ;
System.out.println (red + " are red,") ;
System.out.println (" " + blue + " are blue,") ;
System.out.println (" " + sweet + " and " + sweet2 + " are sweet,") ;
System.out.println (" And so are you, " + name) ;
}
}
I probably don't need the scanner, but I'm using it as a template because we worked on it in class the other day.
 

PsharkJF

Senior member
Jul 12, 2004
653
0
0
I'm assuming your instructor doesn't want you to use GUI-based input (JOptionPane), so I'll demonstrate how to do console input.
 

thraashman

Lifer
Apr 10, 2000
11,112
1,587
126
That is about the only thing I liked about C++ over Java. In C++ include io and then use the "cin" command. In Java you've got to create a buffered reader and it's a good bit trickier to figure out initially. Other than that Java rocks
 

deadlyapp

Diamond Member
Apr 25, 2004
6,656
737
126
Thank you shark. I had someone else mention JOptionPane, which he has not gone over, but I used it.

Your code worked perfectly. Thankkkks
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Originally posted by: PsharkJF
I'm assuming your instructor doesn't want you to use GUI-based input (JOptionPane), so I'll demonstrate how to do console input.

Using the scanner class is probably the way he (edit OPs professor) wanted it done. That said his program runs fine on my computer after I imported Scanner (I assume the version he has does that or it wouldn't have compiled)...