Re: Need some minor help.
Quote:
My project is suppose to look like this
What does the contents of the console window look like when the program is executes?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Re: Need some minor help.
Mine when executed says this;
A B
Project Title
Enter a Fahrenheit temperature (integer): 212
Enter a distance in feet (integer): 100
Enter a weight in pounds (integer): 100
and that would be it.
just above it in the command prompt window it says <terminated> Project 2
I'm not sure if I have some codes out of place or not enough in there.
I also tried taking the conversions and putting them above the last bracket to look like this;
Code :
//Get Input
System.out.print("Enter a Fahrenheit temperature (integer): ");
fTemp = input.nextInt();
System.out.print("Enter a distance in feet (integer): ");
fDist = input.nextInt();
System.out.print("Enter a weight in pounds (integer): ");
pWeight = input.nextInt();
//Perform Conversions
cTemp = fTemp * CS;
mDist = fDist * ME;
kWeight = pWeight * KL;
K = (fTemp + 459.67) * 5/9;
I = fDist * 12;
S = pWeight * 0.0714285714;
}
//Methods
private static void validInput(int i, int j) {
// TODO Auto-generated method stub
boolean withinRange = true;
if (i < 0 || i > 212);
System.out.println("Out of range");
withinRange = false;
}
private static boolean validInput(String entry) {
// TODO Auto-generated method stub
boolean isValid = false;
try {
Integer.parseInt(entry);
isValid = true;
}
catch (Exception ex) {
System.out.println("Entered value is invalid");
}
//Display Report
System.out.println(fTemp + " fahrenheit " + cTemp + " celsius " + K + " Kelvin ");
return isValid;
}
}
But I still get the same result and now I have an error in my display report saying cTemp and K cannot be resolved to a variable.
Re: Need some minor help.
You need to compile the code with the javac program's -Xlint to see a possible problem with the code.
A sample commandline for compiling:
D:\Java\jdk1.6.0_29\bin\javac.exe -Xlint Project2.java
How and when does the code execute the println() statements that will show the results?
Quote:
cTemp and K cannot be resolved to a variable.
You'll have to post all of the code so I can see what the problem is. The last post was only partial code.
Re: Need some minor help.
Ok I got it showing most of the results in my command prompt here is my updated code.
Code :
import java.util.Scanner;
public class Project2 {
private static final Object False = null;
private static final int number = 0;
private static final int CS = 0;
private static final int fTemp = 0;
private static final int fDist = 0;
private static final int ME = 0;
private static final int pWeight = 0;
private static final int KL = 0;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Declare constants
final double CS = (5.0 / 9);
final double ME = 0.3048;
final double KL = 0.4536;
//Declare variables
Scanner input = new Scanner(System.in);
String entry;
int fTemp, fDist, pWeight;
String Inputnumber = null;
double cTemp, mDist, kWeight, K, I, S;
System.out.println(" A B ");
System.out.println(" Project Title ");
//Get Input
System.out.print("Enter a Fahrenheit temperature (integer): ");
fTemp = input.nextInt();
System.out.print("Enter a distance in feet (integer): ");
fDist = input.nextInt();
System.out.print("Enter a weight in pounds (integer): ");
pWeight = input.nextInt();
//Perform Conversions
cTemp = fTemp * CS;
mDist = fDist * ME;
kWeight = pWeight * KL;
K = (fTemp + 459.67) * 5/9;
I = fDist * 12;
S = pWeight * 0.0714285714;
//Display Report
System.out.println(fTemp + " fahrenheit " + cTemp + " celsius " + K + " Kelvin ");
System.out.println(fDist + " feet " + mDist + " meters " + I + " Inches ");
System.out.println(pWeight + " pounds " + kWeight + " kilograms " + S + "Stones ");
}
//Methods
private static void validInput(int i, int j) {
// TODO Auto-generated method stub
boolean withinRange = true;
if (i < 0 || i > 212);
System.out.println("Out of range");
withinRange = false;
}
private static boolean validInput(String entry) {
// TODO Auto-generated method stub
boolean isValid = false;
try {
Integer.parseInt(entry);
isValid = true;
}
catch (Exception ex) {
System.out.println("Entered value is invalid");
}
return isValid;
}
}
Now I believe I need to get the methods (validateInput and Interseparse) that I have under the display results to work and I think that would solve the terminating problem. How would i go about that? if it helps I'm using Eclipse.
These are also my results now;
A B
Project Title
Enter a Fahrenheit temperature (integer): 212
Enter a distance in feet (integer): 100
Enter a weight in pounds (integer): 100
212 fahrenheit 117.77777777777779 celsius 373.15000000000003 Kelvin
100 feet 30.48 meters 1200.0 Inches
100 pounds 45.36 kilograms 7.14285714Stones
Does my question make sense or is it confusing? Im very new to this so the methods are a bit tricky to me, on where to put them for them to execute.
Re: Need some minor help.
Quote:
where to put them for them to execute.
You can put the methods in any order in the class. Their position doesn't effect when they will be executed.
Methods are executed when they are called. If you (or the JVM in some cases) do not call a method, it will not be executed.
Re: Need some minor help.
ok well with my coding in the previous post am I doing it right or have I not called the methods yet?
Re: Need some minor help.
Quote:
have I not called the methods yet?
Where does the code call the methods?
Re: Need some minor help.
I honestly dont know. I thought after i put //Methods and then the codes.
Re: Need some minor help.
Take a look at the tutorial:
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Here is an example of calling a method from your code: It calls the nextInt() method of the Scanner class.
Re: Need some minor help.
I still am getting errors on everything I do.
Re: Need some minor help.
Copy and paste here the text of any error messages you want help with along with the code.
Re: Need some minor help.
Code :
import java.util.Scanner;
public class Project2 {
private static final Object False = null;
private static final int number = 0;
private static final int CS = 0;
private static final int fDist = 0;
private static final int ME = 0;
private static final int pWeight = 0;
private static final int KL = 0;
/**
* @param args
* @return
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
validInput();
//Declare constants
final double CS = (5.0 / 9);
final double ME = 0.3048;
final double KL = 0.4536;
//Declare variables
Scanner input = new Scanner(System.in);
String entry;
int fTemp = 0, fDist, pWeight;
String Inputnumber = null;
double cTemp, mDist, kWeight, K, I, S;
System.out.println(" A B ");
System.out.println(" Project Title ");
//Get Input
System.out.print("Enter a Fahrenheit temperature (integer): ");
fTemp = input.nextInt();
System.out.print("Enter a distance in feet (integer): ");
fDist = input.nextInt();
System.out.print("Enter a weight in pounds (integer): ");
pWeight = input.nextInt();
//Perform Conversions
cTemp = fTemp * CS;
mDist = fDist * ME;
kWeight = pWeight * KL;
K = (fTemp + 459.67) * 5/9;
I = fDist * 12;
S = pWeight * 0.0714285714;
//Display Report
System.out.println(fTemp + " fahrenheit " + cTemp + " celsius " + K + " Kelvin ");
System.out.println(fDist + " feet " + mDist + " meters " + I + " Inches ");
System.out.println(pWeight + " pounds " + kWeight + " kilograms " + S + "Stones ");
}
//Methods
private static void validInput(int i, int j) {
// TODO Auto-generated method stub
boolean withinRange = true;
if (i < 0 || i > 212);
System.out.println("Out of range");
withinRange = false;
}
private static boolean validInput(String entry) {
// TODO Auto-generated method stub
boolean isValid = false;
try {
Integer.parseInt(entry);
isValid = true;
}
catch (Exception ex) {
System.out.println("Entered value is invalid");
}
return isValid;
}
private static void validInput() {
// TODO Auto-generated method stub
}
}
I got no errors in that code but I still cant get the methods to be executed for example if i put in the value 213 it needs to say out of range not convert it.
Re: Need some minor help.
Quote:
it needs to say out of range not convert it.
Where will it print that message? How does the code that prints the message get executed? If you don't call a method, the code in the method will not be executed.
Did you compile the code with javac -Xlint option as I suggested in post#4. It will show you a problem in your code.
Re: Need some minor help.
It needs to print that message in the results. I need the methods to be called on which apparently i havent done right. My methods are after the display report, does that matter? I tried to compile the code with what you posted but even that got an error in Eclipse.
Re: Need some minor help.
Quote:
My methods are after the display report, does that matter?
See post#6
When you get errors, you need to copy the full text of the error messages and paste it here.
Code :
public static void main(String[] args) {
// TODO Auto-generated method stub
validInput();
Here you call the validInput() method BEFORE there is any input to validate. What good can that do?
You need to call the method AFTER the data to be validated has been input
AND you need to pass to the method the data to be validated.
Re: Need some minor help.
Re: Need some minor help.
Quote:
Originally Posted by
Norm
See post#6
When you get errors, you need to copy the full text of the error messages and paste it here.
Code :
public static void main(String[] args) {
// TODO Auto-generated method stub
validInput();
Here you call the validInput() method BEFORE there is any input to validate. What good can that do?
You need to call the method AFTER the data to be validated has been input
AND you need to pass to the method the data to be validated.
Could you by any chance give me an example of doing this the correct way?
Re: Need some minor help.
Here is some pseudo code:
get input from user
validate input
use input in computations
print out results
Re: Need some minor help.
Alright hopefully my last question as im sure this is kind of annoying to you, but heres my code. Now the only errors I am getting now is in bold and bigger font. I put it in the pseudo code that you gave.
Error for the first validInput and guess:
Multiple markers at this line
- Duplicate local variable guess
- Syntax error on token ")", ; expected
- void is an invalid type for the variable
validInput
- Syntax error on token "(", ; expected
Error for the second method:
Multiple markers at this line
- Illegal modifier for parameter validInput; only final is
permitted
- Syntax error on token "(", ; expected
- Syntax error on token ")", ; expected
- Duplicate local variable entry
Multiple markers at this line
- Duplicate local variable
fDist
- Duplicate local variable
pWeight
- Duplicate local variable
fTemp
Void methods cannot return a value
Those are my errors. How may I go about coding this to where those go away?
Code :
import java.util.Scanner;
public class Project2 {
private static final int MAX_VALUE = 212;
private static final int MIN_VALUE = 0;
/**
* @param args
* @return
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Declare constants
final double CS = (5.0 / 9);
final double ME = 0.3048;
final double KL = 0.4536;
//Declare variables
Scanner input = new Scanner(System.in);
String entry;
int fTemp, fDist, pWeight, number, guess = -1;
String Inputnumber = null;
double cTemp, mDist, kWeight, K, I, S;
System.out.println(" A B ");
System.out.println(" Project Title ");
//Get Input
System.out.print("Enter a Fahrenheit temperature (integer): ");
fTemp = input.nextInt();
System.out.print("Enter a distance in feet (integer): ");
fDist = input.nextInt();
System.out.print("Enter a weight in pounds (integer): ");
pWeight = input.nextInt();
//Methods
private static void [B][SIZE=4]validInput[/SIZE][/B](int [SIZE=4][B]guess[/B][/SIZE]) {
// TODO Auto-generated method stub
boolean withinRange = true;
if (guess < MIN_VALUE || guess > MAX_VALUE);
System.out.println("Out of range");
withinRange = false;
validInput();
}
private static boolean [B][SIZE=4]validInput[/SIZE][/B](String [SIZE=4][B]entry[/B][/SIZE]) {
// TODO Auto-generated method stub
int [B][SIZE=4]fTemp, fDist, pWeight[/SIZE][/B];
boolean isValid = false;
try {
fTemp = Integer.parseInt(entry);
fDist=Integer.parseInt(entry);
pWeight=Integer.parseInt(entry);
isValid = true;
}
catch (Exception ex) {
System.out.println("Entered value is invalid");
validInput();
}
[B][SIZE=4]return isValid[/SIZE][/B];
//Perform Conversions
cTemp = fTemp * CS;
mDist = fDist * ME;
kWeight = pWeight * KL;
K = (fTemp + 459.67) * 5/9;
I = fDist * 12;
S = pWeight * 0.0714285714;
//Display Report
System.out.println(fTemp + " fahrenheit " + cTemp + " celsius " + K + " Kelvin ");
System.out.println(fDist + " feet " + mDist + " meters " + I + " Inches ");
System.out.println(pWeight + " pounds " + kWeight + " kilograms " + S + "Stones ");
}
}
private static void validInput() {
// TODO Auto-generated method stub
}
}
Re: Need some minor help.
You have left off the source line numbers for the compiler errors making it hard to find them.
Here is a sample of an error message from the javac command:
Code :
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^
You are having a problem understanding the difference between defining a method and calling it. You can NOT define a method inside of another method:
Code :
public int returnOne(int x) { // define a method
return 1; // return a value /ignores x
} // end returnOne()
You call a method from inside a method:
Code :
public void anotherMethod() {
...
int anInt = returnOne(234); // call returnOne method
...
} //end anotherMethod()
Re: Need some minor help.
Yeah I am.. I'm gonna read up on methods and see if I can figure it. In eclipse to get the source codeeould i run it eoth yhe errors and in the problrms tab copy that?out. This is all a foregin language t
o me. Sorry this message so bad I'm on my phone.
Re: Need some minor help.
Wait until you are at your computer to send messages.
Re: Need some minor help.
Ok Im getting tired of this project and Im sure your getting tired of answering my questions but is this correct now yes or no?
Code :
import java.util.Scanner;
public class Project2 {
private static final int MAX_VALUE = 212;
private static final int MIN_VALUE = 0;
/**
* @param args
* @return
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Declare constants
final double CS = (5.0 / 9);
final double ME = 0.3048;
final double KL = 0.4536;
//Declare variables
Scanner input = new Scanner(System.in);
String entry;
int fTemp, fDist, pWeight, number, guess = -1;
double cTemp, mDist, kWeight, K, I, S;
System.out.println(" A B ");
System.out.println(" Project Title ");
//Get Input
System.out.print("Enter a Fahrenheit temperature (integer): ");
validInput();
System.out.print("Enter a distance in feet (integer): ");
validInput();
System.out.print("Enter a weight in pounds (integer): ");
validInput();
{
//Display Report
System.out.println(fTemp + " fahrenheit " + cTemp + " celsius " + K + " Kelvin ");
System.out.println(fDist + " feet " + mDist + " meters " + I + " Inches ");
System.out.println(pWeight + " pounds " + kWeight + " kilograms " + S + "Stones ");
//Perform Conversions
cTemp = fTemp * CS;
mDist = fDist * ME;
kWeight = pWeight * KL;
K = (fTemp + 459.67) * 5/9;
I = fDist * 12;
S = pWeight * 0.0714285714; }
//Methods
public static void validInput(int guess) {
// TODO Auto-generated method stub
boolean withinRange = true;
if (guess < MIN_VALUE || guess > MAX_VALUE);
System.out.println("Out of range");
withinRange = false;
validInput();
}//end Method
//Next Method
public static boolean validInput(String entry) {
// TODO Auto-generated method stub
int fTemp, fDist, pWeight;
boolean isValid = true;
try {
fTemp = Integer.parseInt(entry);
fDist=Integer.parseInt(entry);
pWeight=Integer.parseInt(entry);
isValid = false;
}
catch (Exception ex) {
System.out.println("Entered value is invalid");
validInput();
}
return isValid;
}
private static void validInput() {
// TODO Auto-generated method stub
}
}
My only error with this when i try to run is after the semicolon in the last conversion..
Description Resource Path Location Type
Syntax error, insert "}" to complete Block Project2.java /Project 2/src line 51 Java Problem
Re: Need some minor help.
Quote:
is this correct now yes or no?
What happens when you compile and execute the code?
If there are errors, then its not correct.
If you get the right answers, then it could be correct.
Where is line 51 that is mentioned in the "error message"??
You need to check the correct pairing of { with a }.
There should be a { at the start of the code for a method definition
and a } at the end of the code for the method.
There should NOT be any methods defined inside of another method.