-
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:
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
-
Hello
What errors, which lines?
-
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:
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?
Code:
cannot find symbol
symbol: method println(java.lang.String,int,java.lang.String)
location: class java.io.PrintStream
-
Ah,
Two ways to do this. One is to concatenate your Strings with the + operation, e.g.,
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.
Code:
System.out.printf("You need: %.2f g of protein per meal%n", protein_and_carb[0] / meals_per_day);
-
The problem is that you used \, which is the escape character, not division, try switching to / instead and see what happens.
-
Quote:
Originally Posted by
nathansizemore
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
-
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:
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:
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:
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!
-
I'm not very familiar with linux, but is the sudo necessary? What happens if you just type
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.
-
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
-
Yeah, it made a Party.class file and that is what I needed to open. Thanks again!