• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Charlene Houchins
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic