Results 1 to 10 of 10

Thread: Introduction

  1. #1
    Join Date
    Apr 2011
    Location
    Dayton, OH
    Posts
    4
    Rep Power
    0

    Default Introduction

    Hello all!
    After browsing the web for a bit, I decided on this place to help me with my future endeavors with Java. Currently, I'm a metal worker, but back in high school I took C++ and Java classes. I just started school and am seeking a Software Development degree. Still doing my basics, no programming classes yet.

    Also, I have a coding related question:

    I started working out, and I was wanting to make a little program that would calculate the amount of carbs and protein I need per meal, based off # of meals I can eat in a day. It has been a while since high school, but I seem to remember a bit from C++, but cannot remember proper Java syntax or remember flow characteristics.

    Here is the code that I have so far:

    Java Code:
    /**
     *
     * @author nathan sizemore
     */
    public class Main
    {
    
        //The basic array that all methods need to pass around
        static int[] protein_and_carb = new int[2];
    
         //Assigns proper values to protein_and_carb[]
         public void basic_calculation()
         {
             int loop_counter = 0;
             double multiplier = 1.5;
    
             while (loop_counter < protein_and_carb.length)
             {
                 protein_and_carb[loop_counter] = protein_and_carb[loop_counter] * multiplier;
                 multiplier += 0.5;
                 loop_counter++;
             }
         }
    
    
        public static void main(String[] args)
        {
            int weight, means_per_day;
    
    
            System.out.println("Weight: ");
            main.weight = System.in.read();
            
            System.out.println("Meals per day: ");
            main.meals_per_day = System.in.read();
            
            protein_and_carb[0] = weight;
            
            basic_calculation(protein_and_carb);
            
            System.out.println("You need: ", protein_and_carb[0], "g of protein daily.");
            System.out.println("You need: ", protein_and_carb[0] \ meals_per_day, "g of protein per meal");
            System.out.println("You need: ", protein_and_carb[1], "g of carbohydrates daily.");
            System.out.println("You need: ", protein_and_carb [1] \ meals_per_day, "g of carbohydrates per meal");
        }
    
    }
    The build errors are all from the main(strings[]) section. I have searched, but cannot seem to figure out what I am doing wrong, or the proper syntax for what I am trying.
    Any help is greatly apprecited!

    Cheers,
    Nathan

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Join Date
    Apr 2011
    Location
    Dayton, OH
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
            System.out.println("You need: ", protein_and_carb[0], "g of protein daily.");
            System.out.println("You need: ", protein_and_carb[0] \ meals_per_day, "g of protein per meal");
            System.out.println("You need: ", protein_and_carb[1], "g of carbohydrates daily.");
            System.out.println("You need: ", protein_and_carb [1] \ meals_per_day, "g of carbohydrates per meal");
    Errors:
    Java Code:
    Created dir: /home/nathan/NetBeansProjects/myPT/build/empty
    Compiling 1 source file to /home/nathan/NetBeansProjects/myPT/build/classes
    /home/nathan/NetBeansProjects/myPT/src/mypt/Main.java:49: illegal character: \92
            System.out.println("You need: ", protein_and_carb[0] \ meals_per_day, "g of protein per meal");
    /home/nathan/NetBeansProjects/myPT/src/mypt/Main.java:49: not a statement
            System.out.println("You need: ", protein_and_carb[0] \ meals_per_day, "g of protein per meal");
    /home/nathan/NetBeansProjects/myPT/src/mypt/Main.java:49: ';' expected
            System.out.println("You need: ", protein_and_carb[0] \ meals_per_day, "g of protein per meal");
    /home/nathan/NetBeansProjects/myPT/src/mypt/Main.java:51: illegal character: \92
            System.out.println("You need: ", protein_and_carb [1] \ meals_per_day, "g of carbohydrates per meal");
    /home/nathan/NetBeansProjects/myPT/src/mypt/Main.java:51: not a statement
            System.out.println("You need: ", protein_and_carb [1] \ meals_per_day, "g of carbohydrates per meal");
    /home/nathan/NetBeansProjects/myPT/src/mypt/Main.java:51: ';' expected
            System.out.println("You need: ", protein_and_carb [1] \ meals_per_day, "g of carbohydrates per meal");
    6 errors
    /home/nathan/NetBeansProjects/myPT/nbproject/build-impl.xml:528: The following error occurred while executing this line:
    /home/nathan/NetBeansProjects/myPT/nbproject/build-impl.xml:261: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 1 second)
    When pressing alt+ender in Netbeans, they give me this, but have no idea of what they are talking about?
    Java Code:
    cannot find symbol
      symbol:   method println(java.lang.String,int,java.lang.String)
      location: class java.io.PrintStream
    Last edited by nathansizemore; 04-30-2011 at 10:00 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah,

    Two ways to do this. One is to concatenate your Strings with the + operation, e.g.,
    Java Code:
    System.out.println("You need: " + protein_and_carb[0] / meals_per_day + "g of protein per meal");

    Also, Java also has a printf, and while I'm not 100% sure, I think that it's somewhat close to C++'s version of this. To learn more about how it works, check out the Formatter API.
    Java Code:
    System.out.printf("You need: %.2f g of protein per meal%n", protein_and_carb[0] / meals_per_day);
    Last edited by Fubarable; 04-30-2011 at 10:03 PM.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The problem is that you used \, which is the escape character, not division, try switching to / instead and see what happens.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nathansizemore View Post
    Java Code:
         //Assigns proper values to protein_and_carb[]
         public void basic_calculation() { ... }
            ...
            basic_calculation(protein_and_carb);
    I'm surprised that the compiler didn't catch this error, i.e. the method basic_calculation() doesn't take any parameter(s). I think there are a few semantic errors there as well (something no compiler can catch).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    Apr 2011
    Location
    Dayton, OH
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the help guys. Although I haven't figured out the exact problem, I have cleaned up the code a bit and think I have it somewhat close to running, but still not there. I decided to read a get a book and start from scratch. After searching around here, I decided to go with Head First Java. Like it so far. I have Netbeans, but cannot get even simple "Hello World" type programs to open up to see if they build? So I decided to switch to Notepad and execute from Terminal and use javac. The same result. They compile, but nothing comes on the screen? Any help would be appreciated. I am using Ubuntu 11.04. This is the source code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class Party 
    {
    	public static void main (String[] args) 
    	{
    		System.out.print("Hello World!");
    	}
    }
    This is what I am doing at the Terminal:
    Java Code:
    nathan@ubuntu:~/Documents$ sudo javac Party.java
    But I get no result. Simply another line to nathan@ubuntu:~/Documents$
    Do I need some type of statement that keeps the window open until I press a key maybe? I tried using System.in.read() afterwards, but it throws up an error:
    Java Code:
    nathan@ubuntu:~/Documents$ sudo javac Party.java
    Party.java:9: unreported exception java.io.IOException; must be caught or declared to be thrown
    		System.in.read();
    Thanks for any help!

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not very familiar with linux, but is the sudo necessary? What happens if you just type
    Java Code:
    javac Party.java
    Also, Head First Java is an excellent beginner book. Instead of making a very long post with all my other suggestions on books, I will link you to a blog post where I give some suggested books: My book suggestions

    The code you have for the hello world is fine, you should be able to compile it fine.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Did your file compile? Do an 'ls' and see if there's a file named Party.class. If it's there run it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Join Date
    Apr 2011
    Location
    Dayton, OH
    Posts
    4
    Rep Power
    0

    Default

    Yeah, it made a Party.class file and that is what I needed to open. Thanks again!

Similar Threads

  1. Introduction
    By goldhouse in forum Lucene
    Replies: 2
    Last Post: 01-17-2010, 12:56 PM
  2. Introduction to my self
    By uthamaputhiran in forum Introductions
    Replies: 0
    Last Post: 02-24-2009, 08:35 AM
  3. Introduction
    By Endex in forum Introductions
    Replies: 2
    Last Post: 11-16-2008, 11:09 AM
  4. Introduction
    By prabitha in forum Introductions
    Replies: 1
    Last Post: 11-03-2008, 05:03 PM
  5. Introduction
    By wedage in forum Introductions
    Replies: 6
    Last Post: 01-23-2008, 05:01 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
  •