Results 1 to 13 of 13
Thread: Simple programing help.
- 09-09-2010, 10:05 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
Simple programing help.
So I'm very very new to java. Taking a class on it and I'm stuck on this programs
The professor is asking:
When displaying the Format, if the format is 'c', then print out "CD" to the screen. If the format is 'm', print out "MP3" to the screen. If the format is 'v', print out "vinyl" to the screen. If it is neither of them, print out "unknown".
Now there's obviously more to the program but this is the only part I cannot complete.
I keep getting "nextChar" is not recognized. I've tried changing "char format;" to "String format;" and then "nextChar" to "next()" and get other errors.
Here is my program so far. What do?
import java.util.Scanner;
public class Album2 {
public static void main(String[] args)
{
String artist, title;
long itemNumber;
int numberOfTracks;
double price;
char format;
System.out.println("Type in a band or musician.");
Scanner keyboard = new Scanner(System.in);
artist = keyboard.next();
System.out.println("Type in an album from them on the next line.");
title = keyboard.next();
itemNumber = 19274676; //pretending program is hooked up to a data base and this is the correct item number.
System.out.println("Item #: " + itemNumber);
System.out.println("How many tracks are on the album?");
numberOfTracks = keyboard.nextInt();
System.out.println("What format would you like this album on?");
System.out.println("Type in 'C' for CD, 'M' for MP3, & 'V' for vinyl.");
format = keyboard.nextChar();
if (format = 'C')
System.out.println("Format: CD");
if (format == M)
System.out.println("Format: MP3");
if (format == V)
System.out.println("Format: Vinyl");
System.out.println("How much does this album cost?");
price = keyboard.nextDouble();
System.out.println("Album: " + artist + " - " + title);
System.out.println("Item number: " + itemNumber);
System.out.println("Track number: " + numberOfTracks);
System.out.println("Format: " + format);
System.out.println("Price: $" + price);
if (price / numberOfTracks <= 1)
System.out.println("This album is on sale!");
else
System.out.println("iTunes is cheaper!");
}
}
- 09-09-2010, 10:13 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
Okay, now I've changed a few things and got the program to run... but now it's ignoring my "Format" input and output.
New and working program:
New and incorrect output:Java Code:import java.util.Scanner; public class Album2 { private static final String C = null; private static final String M = null; private static final String V = null; public static void main(String[] args) { String artist, title; long itemNumber; int numberOfTracks; double price; String format; System.out.println("Type in a band or musician."); Scanner keyboard = new Scanner(System.in); artist = keyboard.next(); System.out.println("Type in an album from them on the next line."); title = keyboard.next(); itemNumber = 19274676; //pretending program is hooked up to a data base and this is the correct item number. System.out.println("Item #: " + itemNumber); System.out.println("How many tracks are on the album?"); numberOfTracks = keyboard.nextInt(); System.out.println("What format would you like this album on?"); System.out.println("Type in 'C' for CD, 'M' for MP3, & 'V' for vinyl."); format = keyboard.next(); if (format == C) System.out.println("Format: CD"); if (format == M) System.out.println("Format: MP3"); if (format == V) System.out.println("Format: Vinyl"); System.out.println("How much does this album cost?"); price = keyboard.nextDouble(); System.out.println("Album: " + artist + " - " + title); System.out.println("Item number: " + itemNumber); System.out.println("Track number: " + numberOfTracks); if (format == C) System.out.println("Format: CD"); if (format == M) System.out.println("Format: MP3"); if (format == V) System.out.println("Format: Vinyl"); System.out.println("Price: $" + price); if (price / numberOfTracks <= 1) System.out.println("This album is on sale!"); else System.out.println("iTunes is cheaper!"); } }
Type in a band or musician.
nirvana
Type in an album from them on the next line.
nevermind
Item #: 19274676
How many tracks are on the album?
13
What format would you like this album on?
Type in 'C' for CD, 'M' for MP3, & 'V' for vinyl.
C
How much does this album cost?
12.99
Album: nirvana - nevermind
Item number: 19274676
Track number: 13
Price: $12.99
This album is on sale!
NOTE: Format is missing, as well as all the C to CD, V to Vinyl commands, etc....
What do?Last edited by koppojutsu; 09-09-2010 at 10:56 PM.
- 09-09-2010, 10:20 PM #3
Scanner class methods can be tricky to use. Scanner reads and buffers what you type in on the console. Some next methods will read up to the end of the line and not read the end of line character. Some hasNext methods will block waiting for you to enter something if there is nothing in the buffer.
Questions/comments about your code:
Where do you assign a value to the C variable?
Use the equals method to compare the contents of two String objects, not the == operator.
When comparing one variable against many choices using if statements, use 'else if 'to test for the next ones after the first one and add an ending else clause to catch the case where none of the previous if tests were true.Last edited by Norm; 09-09-2010 at 10:22 PM.
- 09-09-2010, 10:26 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
I'm trying to make it so you either input "C," "M," or "V" after -
And then display either CD, MP3, or Vinyl here:Java Code:System.out.println("What format would you like this album on?"); System.out.println("Type in 'C' for CD, 'M' for MP3, & 'V' for vinyl."); format = keyboard.next();
Here's the updated codeJava Code:if (format.equalsIgnoreCase(C)) System.out.println("Format: CD"); if (format.equalsIgnoreCase(M)) System.out.println("Format: MP3"); if (format.equalsIgnoreCase(V)) System.out.println("Format: Vinyl");
Java Code:import java.util.Scanner; public class Album2 { private static final String C = null; private static final String M = null; private static final String V = null; public static void main(String[] args) { String artist, title; long itemNumber; int numberOfTracks; double price; String format; System.out.println("Type in a band or musician."); Scanner keyboard = new Scanner(System.in); artist = keyboard.next(); System.out.println("Type in an album from them on the next line."); title = keyboard.next(); itemNumber = 19274676; //pretending program is hooked up to a data base and this is the correct item number. System.out.println("Item #: " + itemNumber); System.out.println("How many tracks are on the album?"); numberOfTracks = keyboard.nextInt(); System.out.println("What format would you like this album on?"); System.out.println("Type in 'C' for CD, 'M' for MP3, & 'V' for vinyl."); format = keyboard.next(); System.out.println("How much does this album cost?"); price = keyboard.nextDouble(); System.out.println("Album: " + artist + " - " + title); System.out.println("Item number: " + itemNumber); System.out.println("Track number: " + numberOfTracks); if (format.equalsIgnoreCase(C)) System.out.println("Format: CD"); if (format.equalsIgnoreCase(M)) System.out.println("Format: MP3"); if (format.equalsIgnoreCase(V)) System.out.println("Format: Vinyl"); System.out.println("Price: $" + price); if (price / numberOfTracks <= 1) System.out.println("This album is on sale!"); else System.out.println("iTunes is cheaper!"); } }Last edited by koppojutsu; 09-09-2010 at 10:57 PM.
- 09-09-2010, 10:28 PM #5
You left off describing what your current problem is.
One out of three is not a very good score. I commented on 3 things, and I see you have made one change.
Also please enclose your code in code tags: Java Forums - BB Code ListLast edited by Norm; 09-09-2010 at 10:31 PM.
- 09-09-2010, 10:41 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
Sorry Norm, I'm far from a java programmer as you can tell.
I did change "==" to "equalsIgnoreCase"
And "if" to "else if"
The current problem is it's not printing out the format type after it's entered.
Current code:
Java Code:import java.util.Scanner; public class Album2 { private static final String C = null; private static final String M = null; private static final String V = null; public static void main(String[] args) { String artist, title; long itemNumber; int numberOfTracks; double price; String format; System.out.println("Type in a band or musician."); Scanner keyboard = new Scanner(System.in); artist = keyboard.next(); System.out.println("Type in an album from them on the next line."); title = keyboard.next(); itemNumber = 19274676; //pretending program is hooked up to a data base and this is the correct item number. System.out.println("Item #: " + itemNumber); System.out.println("How many tracks are on the album?"); numberOfTracks = keyboard.nextInt(); System.out.println("What format would you like this album on?"); System.out.println("Type in 'C' for CD, 'M' for MP3, & 'V' for vinyl."); format = keyboard.next(); System.out.println("How much does this album cost?"); price = keyboard.nextDouble(); System.out.println("Album: " + artist + " - " + title); System.out.println("Item number: " + itemNumber); System.out.println("Track number: " + numberOfTracks); if (format.equalsIgnoreCase(C)) System.out.println("Format: CD"); else if (format.equalsIgnoreCase(M)) System.out.println("Format: MP3"); else if (format.equalsIgnoreCase(V)) System.out.println("Format: Vinyl"); System.out.println("Price: $" + price); if (price / numberOfTracks <= 1) System.out.println("This album is on sale!"); else System.out.println("iTunes is cheaper!"); } }
- 09-09-2010, 10:44 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
My intended output would be:
Album: Nirvana: Nevermind
Item number: 19274676
Track nubmber: 13
Format: Vinyl
Price: $12.99
This album is on Sale!
and what I'm getting is:
Album: Nirvana: Nevermind
Item number: 19274676
Track nubmber: 13
Price: $12.99
This album is on Sale!
For some reason the format input is being ignored and not printed.
- 09-09-2010, 11:07 PM #8
You're making progress. You now have done 1 1/2 of the 3.
I'll copy and paste them here again:
Questions/comments about your code:
Where do you assign a value to the C variable?
Use the equals method to compare the contents of two String objects, not the == operator. >>>> DONE
When comparing one variable against many choices using if statements, use 'else if 'to test for the next ones after the first one and add an ending else clause to catch the case where none of the previous if tests were true. >>>> 1/2 DONE
- 09-09-2010, 11:25 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
I'm assuming I assigned the value to the C variable at the beginning of the code:
Now I don't really understand what C = null means, and that may be the root of my next problem.Java Code:String format, C = null, M = null, V = null;
Currently my code is this:
Now I'm getting it to print the format, but I'm not able to get it to change the input "c" to output "CD". It simply prints out "c". Also just realized I need to code a 4th input option as "unknown" in case the user types something other than C, M, or V. Gonna try and figure that one out right now. But I'm still confused as to why it's not printing out CD, MP3, Vinyl.Java Code:import java.util.Scanner; public class Album2 { public static void main(String[] args) { String artist, title; long itemNumber; int numberOfTracks; double price; String format, C = null, M = null, V = null; System.out.println("Type in a band or musician."); Scanner keyboard = new Scanner(System.in); artist = keyboard.next(); System.out.println("Type in an album from them on the next line."); title = keyboard.next(); itemNumber = 19274676; //pretending program is hooked up to a data base and this is the correct item number. System.out.println("Item #: " + itemNumber); System.out.println("How many tracks are on the album?"); numberOfTracks = keyboard.nextInt(); System.out.println("What format would you like this album on?"); System.out.println("Type in 'C' for CD, 'M' for MP3, & 'V' for vinyl."); format = keyboard.next(); System.out.println("How much does this album cost?"); price = keyboard.nextDouble(); System.out.println("Album: " + artist + " - " + title); System.out.println("Item number: " + itemNumber); System.out.println("Track number: " + numberOfTracks); if (format.equalsIgnoreCase(C)) format = "CD"; else if (format.equalsIgnoreCase(M)) format = "MP3"; else if (format.equalsIgnoreCase(V)) format = "Vinyl"; System.out.println("Format: " + format); System.out.println("Price: $" + price); if (price / numberOfTracks < 1) System.out.println("This album is on sale!"); else System.out.println("iTunes is cheaper!"); } }
- 09-10-2010, 12:02 AM #10
Yes you did give a value to the variable CI assigned the value to the C variable at the beginning of the code:
What do you expect the results of this if test to be?
if (format.equalsIgnoreCase(C))
What will the value of the variable: format be? What value does the above if test for?
Doesn't C need to have some special value instead of null?
Perhaps if you changed the name of the variable from C to codeForCD it would make more sense.
Go back and read two of my previous posts. I told you what you needed to do at the end of the if/else if statements.I need to code a 4th input option as "unknown"
- 09-10-2010, 12:20 AM #11
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
Wooohoooo!
Thanks for your help Norm.
I got the code working. Went a different path than if-else statements.
Here's the code if anyone's interested.
Java Code:import java.io.IOException; import java.util.Scanner; public class Album2 { public static void main(String[] args) throws IOException { String artist, title; long itemNumber; int numberOfTracks; double price; char format; System.out.println("Type in a band or musician."); Scanner keyboard = new Scanner(System.in); artist = keyboard.nextLine(); System.out.println("Type in an album from them on the next line."); title = keyboard.nextLine(); itemNumber = 19274676; //pretending program is hooked up to a data base and this is the correct item number. System.out.println("Item #: " + itemNumber); System.out.println("How many tracks are on the album?"); numberOfTracks = keyboard.nextInt(); System.out.println("What format would you like this album on?"); System.out.println("Type in 'C' for CD, 'M' for MP3, & 'V' for vinyl."); format = (char)System.in.read(); System.out.println("How much does this album cost?"); price = keyboard.nextDouble(); System.out.println("Album: " + artist + " - " + title); System.out.println("Item number: " + itemNumber); System.out.println("Track number: " + numberOfTracks); switch (format) { case 'c': case 'C': System.out.println("Format: CD"); break; case 'm': case 'M': System.out.println("Format: MP3"); break; case 'v': case 'V': System.out.println("Format: Vinyl"); default: System.out.println("Format: Unknown"); } System.out.println("Price: $" + price); if (price / numberOfTracks < 1) System.out.println("Yay! This album is on sale; which mean you get to go to a record store!"); else System.out.println("iTunes is cheaper... but who cares, support your local record store and go buy there!"); } }
- 09-10-2010, 12:27 AM #12
Yes, the switch statement works well where the variable can promote to an int value (char promotes to an int)
- 09-11-2010, 01:28 AM #13
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
java programing homework help!
By mxrider in forum New To JavaReplies: 17Last Post: 02-09-2010, 03:23 AM -
Programing Technique question - Try or If
By TimHuey in forum New To JavaReplies: 6Last Post: 09-15-2009, 10:03 PM -
socket programing outside the network
By Omarero in forum NetworkingReplies: 4Last Post: 02-16-2009, 06:15 AM -
Hi, I am new to programing!
By Zrob in forum IntroductionsReplies: 1Last Post: 09-14-2008, 05:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks