User Input
Author |
Message |
fandanstan
|
Posted: Wed Jan 16, 2008 12:35 am Post subject: User Input |
|
|
Ok, so i'm doing this project for my Comp Sci class. Our teacher hasn't taught us anything, and he expects us to know how to do this. Anyways, i need help on getting the user to input something, and it displays on the screen. I finally found out how to input numbers and stuff, but when you input a number, i want that number that corresponds with the product to show up with its price. This is what i have so far:
public class Final_Project
{
public static void main (String[] str)
{
System.out.println ("Hello, what kind of clothing are you interested in?");
System.out.println ("----------------------------------------------------");
System.out.println (" (1) 60's Caravelli Suit XL - $179.99");
System.out.println (" (2) 70's Vintage Coat L - $189.99");
System.out.println (" (3) Tan Cashmere Blazer/Jacket 40 R/REG - $159.99");
System.out.println (" (4) Vintage 60's Suit Dress & Coat XS - $125.99");
System.out.println (" (5) 60's Dalton Cashmere Crochet Sweater - $129.99");
System.out.println (" (6) 70's Italian Sweater - $109.99");
System.out.println (" (7) Gucci Handbag - $99.99");
System.out.println (" (8) 70's Garolini Shoes - $159.99");
System.out.println (" (9) 70's Sparkle Gem Dress - $169.99");
System.out.println (" (10) Men's Dress Shoes - $149.99");
System.out.println (" (11) Sorry, I'm not interested");
String input = "";
boolean run = true;
int items[] = new int [10];
int num = 0;
}
} |
|
|
|
|
|
Sponsor Sponsor
|
|
|
syntax_error
|
Posted: Wed Jan 16, 2008 12:43 am Post subject: Re: User Input |
|
|
the wind whispers can you hear it
it says use the case structure ... then its gone...
Edit: it also tell me more... code tags... and stop blaming your problems on your teacher ... the internet is here to help.... |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Wed Jan 16, 2008 9:51 am Post subject: Re: User Input |
|
|
syntax_error @ Wed Jan 16, 2008 12:43 am wrote: use the case structure ... You can use an array too (just check the input first). |
|
|
|
|
|
HeavenAgain
|
Posted: Wed Jan 16, 2008 11:33 am Post subject: RE:User Input |
|
|
i believe this man here is asking how to get input, not how to "check" what input, however it will help
and for getting input, i can suggest you to something simple, using the Scanner class, heres an example
code: |
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
String line = input.nextLine();
int number = input.nextInt();
System.out.println(line+number);
}
} |
|
|
|
|
|
|
fandanstan
|
Posted: Fri Jan 18, 2008 6:59 pm Post subject: Re: User Input |
|
|
Ok, i've used that code, i'm kinda a newb at this. Say, for example if the user inputted the number 1, how would i make it so that the first product is shown?
import java.util.Scanner;
import java.io.*;
public class Final_Project
{
public static void main (String str[])
{
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println ("Hello, please select the number which is assigned to the product you are interested in.");
System.out.println ("---------------------------------------------------------------------------------------");
System.out.println (" (1) 60's Caravelli Suit XL - $179.99");
System.out.println (" (2) 70's Vintage Coat L - $189.99");
System.out.println (" (3) Tan Cashmere Blazer/Jacket 40 R/REG - $159.99");
System.out.println (" (4) Vintage 60's Suit Dress & Coat XS - $125.99");
System.out.println (" (5) 60's Dalton Cashmere Crochet Sweater - $129.99");
System.out.println (" (6) 70's Italian Sweater - $109.99");
System.out.println (" (7) Gucci Handbag - $99.99");
System.out.println (" (8) 70's Garolini Shoes - $159.99");
System.out.println (" (9) 70's Sparkle Gem Dress - $169.99");
System.out.println (" (10) Men's Dress Shoes - $149.99");
System.out.println (" (11) Sorry, I'm not interested");
System.out.println("Which item would you like? ");
Scanner input = new Scanner (System.in);
String line = input.nextLine();
int number = input.nextInt();
System.out.println(line+number);
}
}
|
|
|
|
|
|
syntax_error
|
Posted: Fri Jan 18, 2008 7:33 pm Post subject: Re: User Input |
|
|
err why are you using BufferedReader?/
you already have a scanner dont you dont need the BufferedReader
Java: |
import java.util.Scanner;
//import java.io.*;
public class FinalProject // dont use underscores
{
public static void main (String str [])
{
Scanner sc = new Scanner (System. in);
// BufferedReader read=new BufferedReader(new InputStreamReader(System.in)); dont need at all
System. out. println ("Hello, please select the number which is assigned to the product you are interested in.");
System. out. println ("---------------------------------------------------------------------------------------");
System. out. println (" (1) 60's Caravelli Suit XL - $179.99");
System. out. println (" (2) 70's Vintage Coat L - $189.99");
System. out. println (" (3) Tan Cashmere Blazer/Jacket 40 R/REG - $159.99");
System. out. println (" (4) Vintage 60's Suit Dress & Coat XS - $125.99");
System. out. println (" (5) 60's Dalton Cashmere Crochet Sweater - $129.99");
System. out. println (" (6) 70's Italian Sweater - $109.99");
System. out. println (" (7) Gucci Handbag - $99.99");
System. out. println (" (8) 70's Garolini Shoes - $159.99");
System. out. println (" (9) 70's Sparkle Gem Dress - $169.99");
System. out. println (" (10) Men's Dress Shoes - $149.99");
System. out. println (" (11) Sorry, I'm not interested");
System. out. println("Which item would you like? ");
String line = input. nextLine();
int number = input. nextInt();
if (number == 1) { do this... }
// and so on i dont wish to do the whole code for you so its sorta a start then
//keep adding teh value of item one to a total var that way you can get teh total cost of all teh shopping
// and soo on you can use if staments as well
}
}
|
|
|
|
|
|
|
HeavenAgain
|
Posted: Fri Jan 18, 2008 7:35 pm Post subject: RE:User Input |
|
|
sigh....
have you even tried out the code? as you can see, when you declear a Scanner class, and use nextInt() method, you can get the integer input from the user
and after that is just simple if statement and how come in your code, you have " BufferedReader read=new BufferedReader(new InputStreamReader(System.in)); " AND Scanner? they are both inputs..... you only need 1....
and please, dont tell me you dont know how to use if statement in java, because this is your "final_project" afterall....
or you can do the array way, all you need is 2 parallel array for the price, and the item, it will save the if statements.... but go with the easy method first.... |
|
|
|
|
|
|
|