• 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

Please help for upcoming college test

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a practical programming test coming up. There is a sample test that I am working through and I am stuck on two of the programs. I would appreciate any help.

The first program should read a name and output if it is short (3 letters or less) long (8 letters or more) or average (4-7).
This is the code I have been trying:
But that won't work for me it says "operator < cannot be applied to java.lang.String,int"

The other program asks that you input 4 numbers all in the same line. Ex. 40 50 60 70 and the computer must output the difference between the biggest and smallest. Ex. 40 and 70 difference = 30. How would you go about that?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But that won't work for me it says "operator < cannot be applied to java.lang.String,int"


That's because 'one' is of type string and so you can't compare it to a type int.
I notice you have called the string's length() method but you haven't assigned the returned value to anything. I suggest you assign this value to a variable of type int and use that in your size comparisons.
 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.



Thanks I didn't know about that. I'll make sure to use them in the future.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The other program asks that you input 4 numbers all in the same line. Ex. 40 50 60 70 and the computer must output the difference between the biggest and smallest. Ex. 40 and 70 difference = 30. How would you go about that?


You shouldn't even be thinking about this problem until you have solved the first one. Learning to program is hard enough without trying to write 2 different programs at the same time.
 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

But that won't work for me it says "operator < cannot be applied to java.lang.String,int"


That's because 'one' is of type string and so you can't compare it to a type int.
I notice you have called the string's length() method but you haven't assigned the returned value to anything. I suggest you assign this value to a variable of type int and use that in your size comparisons.



Thanks I'll give that a try.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one:
could be replaced by or at least It is not necessary to initialize a variable right after declaring it. There is no point in creating an object you'll never use.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
…particularly new String();
If you look for that String constructor, it says there is usually no point in using it. String has thirteen constructors, so presumably whoever wrote it is not superstitious There are also two deprecated constructors and those thirteen include two which you are told not to use.

The reason for so many constructors is that library class design is rather different from designing your own classes. When you design your classes, you give them the smallest public interface consistent with their functioning at all.
When you design a library class, you tend to give it a larger public interface. If somebody goes and tells the designers, “Maybe somebody will need a String constructor taking a Cucumber and an Onion parameter,” and you will find public String(Cucumber c, Onion o) appears as a constructor.
 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. Btw I'm only just past the "Hello world" program. So my programming skills and Knowledge are very minimal.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cameron Finch wrote:Thanks guys. Btw I'm only just past the "Hello world" program. So my programming skills and Knowledge are very minimal.


That's OK, we were all at that stage once upon a time. The more you practice the quicker you will learn and we are always here to help when you get stuck, confused, frustrated etc
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your first problem, hint has been already given. You need to check the length of string, so that you first need to get the length the string.



I hope you can now guess what you need to do.
 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I got it to work, by converting the length to an int. One more to go!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic