File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes User inputs string to get calculated output- am I heading in the right direction? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "User inputs string to get calculated output- am I heading in the right direction?" Watch "User inputs string to get calculated output- am I heading in the right direction?" New topic
Author

User inputs string to get calculated output- am I heading in the right direction?

Charlene Houchins
Greenhorn

Joined: May 01, 2013
Posts: 4

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Welcome to the Ranch.

At first sight, it looks like the logic of the program is OK.

However, I see some things that are not valid Java code. For example, lines 41 and 42:

Did you try to compile your source code? You would have gotten errors.

When you write a program, don't write a whole bunch of code at once. Write a few lines, try to compile it, and see if the compiler reports any errors. If there are errors, fix them first. If you write a long and complicated program and you try compiling it afterwards, and you have errors, it will be much harder to understand what causes the compile errors.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Charlene Houchins
Greenhorn

Joined: May 01, 2013
Posts: 4
Jesper de Jong wrote:Welcome to the Ranch.

At first sight, it looks like the logic of the program is OK.

However, I see some things that are not valid Java code. For example, lines 41 and 42:

Did you try to compile your source code? You would have gotten errors.

When you write a program, don't write a whole bunch of code at once. Write a few lines, try to compile it, and see if the compiler reports any errors. If there are errors, fix them first. If you write a long and complicated program and you try compiling it afterwards, and you have errors, it will be much harder to understand what causes the compile errors.





I ran it up to the strings and all of that is fine. I understand the logic. What I need help with is how to actually say what I'm trying to say in java code. My first question is how do you define two words (strings, I'm assuming) that will be input from the user. And if the user does not enter either one (has to be both), output will say enter the right word. When they do enter the right word, I want the calculation that I have defined for that word to print out along with "your answer is...".

Errors that show up:

C:\Users\Charley\Documents\CSC 110\Programs\Un.java:57: error: '(' expected
if else (input.equals("person")){
^
C:\Users\Charley\Documents\CSC 110\Programs\Un.java:57: error: illegal start of expression
if else (input.equals("person")){
^
C:\Users\Charley\Documents\CSC 110\Programs\Un.java:57: error: ')' expected
if else (input.equals("person")){
^
C:\Users\Charley\Documents\CSC 110\Programs\Un.java:57: error: ';' expected
if else (input.equals("person")){
^
C:\Users\Charley\Documents\CSC 110\Programs\Un.java:66: error: reached end of file while parsing
}
^
5 errors

Tool completed with exit code 1

So if I go thru it step by step, the first, most important question is how I define those two words so they are linked to these formulas (total = band + totalplate + totalrent and person= total/person) and when they are entered they are calculated and output the answer.
Charlene Houchins
Greenhorn

Joined: May 01, 2013
Posts: 4
C:\Users\Charley\Documents\CSC 110\Programs\Un.java:41: error: cannot find symbol
String.total = "total";
^
symbol: variable total
location: class String
C:\Users\Charley\Documents\CSC 110\Programs\Un.java:42: error: cannot find symbol
String.person = "person";
^
symbol: variable person
location: class String
2 errors

Tool completed with exit code 1


When I deleted everything below the strings this is what showed up. Like I said I don't know how to define them and help with that would be greatly appreciated!
Charlene Houchins
Greenhorn

Joined: May 01, 2013
Posts: 4
Okay, I checked everything up to the while statement and it ran fine.

C:\Users\Charley\Documents\CSC 110\Programs\Un.java:56: error: cannot find symbol
}while(!String.total("total")&& !String.person("person"));
^
symbol: method total(String)
location: class String
C:\Users\Charley\Documents\CSC 110\Programs\Un.java:56: error: cannot find symbol
}while(!String.total("total")&& !String.person("person"));
^
symbol: method person(String)
location: class String
2 errors

Tool completed with exit code 1

I don't know how to word this part exactly (meaning I don't know the exact java code[language] appropriate for this statement).
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

The things that I see that are wrong in your code above are:

Lines 41 and 42: This is just not valid Java code. What did you mean exactly with these two lines? Can you explain each of the parts of these lines? For example, what does the "String." mean? What did you intend with the "= int total;"?

A variable declaration looks like this:

Type name = value;

where Type is the type of the variable (for example, String or int), name is the name of the variable, and value is the value that you want to initialize the variable with.

Your lines 41 and 42 don't look like this. Look at the other places in your program where you declare variables: lines 12 - 15, 27 - 29. Do you see that your lines 41 and 42 don't look the same way?

See Variables in Oracle's Java Tutorials to learn how to work with variables.

The curly braces in lines 53 - 65 are unbalanced. You open a curly brace { at the end of line 51, but the closing curly brace }, which should have been on line 56, is missing. Likewise on lines 57 and 60.

Take it one problem at a time. Don't try to fix many things at once, because it will be confusing.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8180
    
  23

Charlene Houchins wrote:Like I said I don't know how to define them and help with that would be greatly appreciated!

Well, String is the name of a class, so the only thing that String.total would be used for is to reference a static attribute called 'total' in the String class - and I can tell you for nothing: there ain't no such animal.

I hate to say, but it sounds to me like you may be trying too much too soon. If you don't know how to declare Strings properly, trying to do things like getting user input with Scanners is going to be a pretty tall order.

The best advice I can give you is to read the tutorials on the subject.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Never use \n unless you have been told you require the LF character. Use println, or after printf, use %n, which you can read about here and here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: User inputs string to get calculated output- am I heading in the right direction?