Results 1 to 13 of 13
  1. #1
    koppojutsu is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default 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!");



    }
    }

  2. #2
    koppojutsu is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    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:
    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!");
    		
    		
    		
    	}
    }
    New and incorrect output:
    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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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.

  4. #4
    koppojutsu is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    I'm trying to make it so you either input "C," "M," or "V" after -
    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();
    And then display either CD, MP3, or Vinyl here:
    Java 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");
    Here's the updated 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");
    		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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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 List
    Last edited by Norm; 09-09-2010 at 10:31 PM.

  6. #6
    koppojutsu is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    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!");
    		
    		
    		
    	}
    }

  7. #7
    koppojutsu is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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

  9. #9
    koppojutsu is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    I'm assuming I assigned the value to the C variable at the beginning of the code:
    Java Code:
    		String format, C = null, M = null, V = null;
    Now I don't really understand what C = null means, and that may be the root of my next problem.

    Currently my code is this:
    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!");
    		
    		
    		
    	}
    }
    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.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I assigned the value to the C variable at the beginning of the code:
    Yes you did give a value to the variable C

    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.

    I need to code a 4th input option as "unknown"
    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.

  11. #11
    koppojutsu is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    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!");
    		
    		
    		
    	}
    }

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Yes, the switch statement works well where the variable can promote to an int value (char promotes to an int)

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    Yes, the switch statement works well where the variable can promote to an int value (char promotes to an int)
    Learn something new everyday, strange since c++ would suggest the same and im trying to learn that too, still would not have done it this way with diff checks for lowercase and uppercase chars but that is more refinement than meeting requirements :)

Similar Threads

  1. java programing homework help!
    By mxrider in forum New To Java
    Replies: 17
    Last Post: 02-09-2010, 03:23 AM
  2. Programing Technique question - Try or If
    By TimHuey in forum New To Java
    Replies: 6
    Last Post: 09-15-2009, 10:03 PM
  3. socket programing outside the network
    By Omarero in forum Networking
    Replies: 4
    Last Post: 02-16-2009, 06:15 AM
  4. Hi, I am new to programing!
    By Zrob in forum Introductions
    Replies: 1
    Last Post: 09-14-2008, 05:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •