Results 1 to 14 of 14

java

This is a discussion on java within the Tech Support forums, part of the Knight Online (ko4life.com) category; All chocolate bars today cost $0.99. Ask for the name of any chocolate bar to be entered. Also ask for ...
Page: 1


  1. #1
    Banned
    Join Date
    Dec 2007
    Posts
    1,195

    Default

    All chocolate bars today cost $0.99. Ask for the name of any chocolate bar to be entered. Also ask for the quantity of bars wanted. Leave a blank line, then use "\t" to display the name, quantity, and total cost, with the heading in capital, as follows...

    CHOCOBAR________QUANTITY_________ COST

    Kitkat_____________3______________$2.97

    (stupid forum wont let me use more then one space in between so i had use "_")

    Code:
    import javax.swing.JOptionPane;
    
    
    public class Chocs 
    {
    ****public static void main(String[] args)
    ****{
    ********String name = JOptionPane.showInputDialog("Chocolate bar name?");
    ********
    ********String bars = JOptionPane.showInputDialog("How many?");
    ********int chocob = Integer.parseInt(bars);
    ********int costtot = (int) (chocob * 0.99);
    ********String line = " CHOCBAR********QUANTITY********COST";
    ********String line1 = name+****bars+****costtot;
    ********System.out.println(line);
    ********System.out.println(line1);
    ********
    ********System.exit(0);
    ********
    ****}
    
    }
    Okay i got a two problems here...

    - out put for costtot is being rounded to the nearest whole number
    - my out put is looking like this, i think it might be because i didn't add "\t" ? i dont know what \t does?



    its all crunched up instead of having the spacing i added in String line1


    P.S. its homework if you where wondering whats up with the choc bars lol

  2. #2
    Banned
    Join Date
    Dec 2007
    Posts
    1,195

    Default

    :huh:

  3. #3
    VIP Member Senior Member JaCoX's Avatar
    Join Date
    Sep 2006
    Location
    Lima - Perú
    Posts
    1,456

    Default

    i want a chocholate bar

  4. #4
    Banned
    Join Date
    Dec 2007
    Posts
    1,195

    Default

    okay fixed it. just need to know why the spacing is coming out all fuked up on on line1

    Code:
    import javax.swing.JOptionPane;
    
    
    public class Chocs 
    {
    ****public static void main(String[] args)
    ****{
    ********String name = JOptionPane.showInputDialog("Chocolate bar name?");
    ********
    ********String bars = JOptionPane.showInputDialog("How many?");
    ********int chocob = Integer.parseInt(bars);
    ********double costtot = (double) (chocob * 0.99);
    ********String line = " CHOCBAR********QUANTITY********COST";
    ********String line1 = name+ ****bars+****costtot;
    ********System.out.println(line);
    ********System.out.println(line1);
    ********
    ********System.exit(0);
    ********
    ****}
    
    }

  5. #5
    Banned
    Join Date
    Dec 2007
    Posts
    1,195

    Default

    i want a chocholate bar[/b]
    looks like neither of us will be getting a chocbar today ;(

  6. #6
    Banned
    Join Date
    Dec 2007
    Posts
    1,195

    Default


  7. #7
    Hustlin' Senior Member kgllegend's Avatar
    Join Date
    Jan 2007
    Posts
    619

    Default

    Lol dude, don't ask for coding help on Ko4Life. LOL...

  8. #8
    PriestOfShadows
    Guest

    Default

    i know some forums that could help. but youll have tp pm me cuz they involve hacking crap.

  9. #9
    Protoss Arbiter Senior Member
    Join Date
    Mar 2006
    Location
    California
    Posts
    2,296

    Default

    First of all, in your original code, you're declaring costtot as an integer, when it should be either a double or a float (you are multiplying by $0.99, so it's going to be pretty rare that you will get an integer as an answer that is exact enough).

    Second, your spaces inbetween your code don't work like that. You have spaces inbetween the variables that is part of the code (which Java skips and treats as one space). You need to concatenate a STRING that has a bunch of spaces. So:

    Code:
    String line1 = name+**** bars+****costtot;
    is not correct.

    Code:
    String line1 = name+ "****"+ bars+ "********"+ costtot;
    is what it should be. (I'm actually not sure about the syntax, I haven't coded in Java in awhile, I code in Scheme (dialect of LISP) right now so you can work out the concatenation syntax, but that's the general idea. You need to concatenate a string of those spaces.)

  10. #10
    Banned
    Join Date
    Dec 2007
    Posts
    1,195

    Default

    Thank you lutz, i actually tried " " before but got an error because i forgot to add the +&#39;s <,<

  11. #11
    evilwevel
    Guest

    Default

    Code:
    String line1 = name+ "****"+ bars+ "********"+ costtot;
    shouldn&#39;t this be

    Code:
    String line1 = name + "\t" + bars + "\t" + costtot;
    since your teacher said you had to use tab for the spaces ?

    anyway if you have anymore questions i&#39;m pretty sure there are enough people here that could answer unlike legend1492 said.

  12. #12
    Banned
    Join Date
    Dec 2007
    Posts
    1,195

    Default

    Code:
    String line1 = name+ "****"+ bars+ "********"+ costtot;
    shouldn&#39;t this be

    Code:
    String line1 = name + "\t" + bars + "\t" + costtot;
    since your teacher said you had to use tab for the spaces ?

    anyway if you have anymore questions i&#39;m pretty sure there are enough people here that could answer unlike legend1492 said.[/b]
    doesn&#39;t work, im pretty sure no string or command works in " "s

  13. #13
    evilwevel
    Guest

    Default

    yeah sorry, forgot that

    it does work, but only with chars i think.

    and it has to be &#39;\t&#39; instead of "\t" (or atleast thats what i&#39;ve come up with after trying to remember :lol: ) just go for the spaces :P

  14. #14
    paperownz
    Guest

    Default

    Umm i Did java last yr, but it was pain in a neck. I will c if i can figure it out with my high school java knowledge.

Bookmarks

Posting Permissions

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