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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

why does it tell me that there is else without if (java programming)?

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

what i am trying to do here. is to let the user type a hexadecimal number

and then display it.


it tells me that there is an else with no if.. why is that?
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
HINT...

How many line(s) of code does an IF block consist of without the use of '{' and '}' ??
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Let's add some brackets (and indentation) to that code to show you how the compiler sees it:
Just add those brackets yourself, and put the System.out inside them. This is also why I always use blocks, even if there is only one statement. If you add another statement the code will keep working, unlike your example.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
another problem that i have is..

if(c!=number)

it doesnt let me use c in a boolean sentence.

but i must get a letter from the user and convert it to a number
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You could use e.g. int newC = Integer.parseInt(c);
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Rene Larsen wrote:You could use e.g. int newC = Integer.parseInt(c);



but that newC would have to be the letter the user typed.

the if method, wont let newC inside


may be this format could be better. in case the user enters random charactaristics.. what do you think?

 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
why not??

It is the same you are doing with the number variable - and two int compared with each other should work in an IF
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please, please try to indent your code in a meaningful, consistent manner--without proper indentation it's quite difficult to understand.

Also note that you can print the hexadecimal representation of a number a bit more concisely than in the above code.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

David Newton wrote:Please, please try to indent your code in a meaningful, consistent manner--without proper indentation it's quite difficult to understand.

Also note that you can print the hexadecimal representation of a number a bit more concisely than in the above code.



true. but i thought the switch method is good, cause it has a default line.

what if the user will write 2000 or hello?

whats then?


i need to convert a letter to a number right now. and while c is set as an input, it isnt working.

i wonder how i could make it work


i corrected my second code:



but it tells me it cant initialize number2. why?
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What is it exactly you want to do??

Do you want to compare a fixed String (String abc) with the input - or just check if the input is valid??
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This line makes no sense:Or rather it does, but is almost certainly not what you mean.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

David Newton wrote:This line makes no sense:Or rather it does, but is almost certainly not what you mean.






I corrected it. but i still have a problem with the first if. i need it to recognise that i entered a character and then apply the calculation
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The first error you need to fix, is this line

here you'll get a NumberFormatException if you enter a letter.

You need to tell parseInt that it is a hex (HINT: base 16)

PS: I don't get an error, with the first if.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Rene Larsen wrote:The first error you need to fix, is this line

here you'll get a NumberFormatException if you enter a letter.

You need to tell parseInt that it is a hex (HINT: base 16)

PS: I don't get an error, with the first if.




this doesnt have an error. but it refuses to recognise letters



no idea why
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
because you are using the Integer.parseInt(String s) method. This clearly states:

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value



Perhaps there is another method in the Integer class you could use...or an overloaded one...
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:because you are using the Integer.parseInt(String s) method. This clearly states:

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value



Perhaps there is another method in the Integer class you could use...or an overloaded one...




there is an easy code i wrote



it should work, but it doesnt. it stops and gives me 0 on each value no matter what the value is.. why?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Why "should" it work? Have you traced out the program's execution with paper and pen[cil]?
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

David Newton wrote:Why "should" it work? Have you traced out the program's execution with paper and pen[cil]?





what is missing?

it writes only the letter b as 10
and with others it writes nothing
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

David Newton wrote:Have you traced out the program's execution with paper and pen[cil]?


Maybe follow up with a post with what you think happens for a few other values--try tracing the program's execution by hand.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I took your code as posted above, changed it to 'public' so it could run, and ran it.

When I gave it an input of 'b', I got nothing (which is what i expected).

When i gave it an input of 'a', i got '9' (which, again, is what i expected).

I would suggest you put a ton of 'System.out.println()' statements in there, and run it again, to see what it is really doing.

Note that you're line 19 is meaningless. c will ALWAYS equal c.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:I took your code as posted above, changed it to 'public' so it could run, and ran it.

When I gave it an input of 'b', I got nothing (which is what i expected).

When i gave it an input of 'a', i got '9' (which, again, is what i expected).

I would suggest you put a ton of 'System.out.println()' statements in there, and run it again, to see what it is really doing.

Note that you're line 19 is meaningless. c will ALWAYS equal c.




what do i put in the if function?


i mean, i cant compare a character with a number. cause if i could i would do it.

i need to make if" method understand that whenever a character is entered, he needs to convert it to a number.
if there is an integral. just print it.

why do i get nothing and what do i write in the if function
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Have you read what I wrote ??

HINT: use base 16 with parseInt
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

import java.io.*;
class Exxx3
{
public static void main(String[] args)
{
Console console=System.console();
System.out.println("please type a hexadecimal number");
String input;
input=console.readLine();
int count=1;
int count2=0;
String hex = "0123456789abcdef";

while(input.charAt(0)!=hex.charAt(count))
{count2=count+1;
count++;
}
System.out.println("your decimal number is"+" "+count2);


}
}



okay. i did it. it works. i can convert from hex to dec with this application above and from dec to hex with that application below




but how can i prevent the user from writing something insane like 2000..a character which isnt there. how do i do input monitoring?
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Dmitri Makovetskiy wrote:i mean, i cant compare a character with a number. cause if i could i would do it.



Why can't you?


C:\slop>java Exx2
Yup
Nope
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:

Dmitri Makovetskiy wrote:i mean, i cant compare a character with a number. cause if i could i would do it.



Why can't you?


C:\slop>java Exx2
Yup
Nope



Fred, i finished my 2 tasks. i posted them above yoru message.

could you help me finding some method that will prevent the user typing something that isnt allowed.. like a character...dkaldaalsda or number 20000000
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Dmitri Makovetskiy wrote:

fred rosenberger wrote:

Dmitri Makovetskiy wrote:i mean, i cant compare a character with a number. cause if i could i would do it.



Why can't you?


C:\slop>java Exx2
Yup
Nope



Fred, i finished my 2 tasks. i posted them above yoru message.

could you help me finding some method that will prevent the user typing something that isnt allowed.. like a character...dkaldaalsda or number 20000000



2000 IS allowed - this is a real hex number - but yxz isn't...

What if your code look like this - what would then happen??
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
before we can do that, you have to tell us what is and is not allowed. 20000000 seems like a perfectly valid number to me, hex or decimal.

did you look at the API for the Integer class I linked to above? Did you see ANYTHING that might help you? Between me and Rene, it should be pretty obvious.

Go look there, and tell us what you find.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:before we can do that, you have to tell us what is and is not allowed. 20000000 seems like a perfectly valid number to me, hex or decimal.

did you look at the API for the Integer class I linked to above? Did you see ANYTHING that might help you? Between me and Rene, it should be pretty obvious.

Go look there, and tell us what you find.



i dont remember if i looked. i think i did.

my instruction was to limit the user to writing numbers/characters from 1-16. now i am thinking of such rule..
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Dmitri Makovetskiy wrote:i dont remember if i looked. i think i did.


Look now, and let us know what you found that's relevant to what we're trying to help you with.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


i am on the second part of this task. i need to establish rules for the user to not put invalid characters.. such as adlakldadsad or 2000

Do you think i have got mistakes? since i put if inside and outside of loops. it is a bit messy , i know. but if i could make it work, it could be great!!
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Is it in your would only allowed to enter one hex digit/letter??

The value 2000 is a valid hex....

Do you read what has been posted in this thread??
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This code confuses me (apart from being difficult to read because of the poor indentation, which I've fixed here):Now, you *know* the body of the if statement is *always* going to execute, because it's the same condition as in your while statement--so why do them both? (And why bother using count + 1 if you just incremented count with the ++ operator? But that's relatively minor.)

I do not believe that you have traced the execution of your program using a pad of paper and a pen[cil]. You really, really should--just writing code at random simply doesn't work--it'd be easier if you took the time to think things through and understand what your code is doing.

This is just my opinion, and you're free to continue ignoring it.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Rene Larsen wrote:Is it in your would only allowed to enter one hex digit/letter??

The value 2000 is a valid hex....

Do you read what has been posted in this thread??



okay, it doesnt matter..

i need to make some rules about not putting weird characters...such as .."adkaldadakaldcx"


i need to remove a few things.

i need to think how i can convert one number to another..

then make a rule not to put weird characters




i did this. i used a few whiles. but i think i must use ifs in the middle..>
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Dmitri Makovetskiy wrote:

Rene Larsen wrote:
Do you read what has been posted in this thread??



okay, it doesnt matter..




It doesn't matter whether you have read the previous posts or not? Why not?

Henry
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Have you tried the code I posted??

This should give you an idea, so you can do what you want.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Rene Larsen wrote:Have you tried the code I posted??

This should give you an idea, so you can do what you want.



i am in a direction of this code:



i want to convert all those while loops to for. this would allow me to use if and else.

i must have if and else's.

The structure should be like that.
if
the letter appears in the string
print it


if it doesnt
ask for another number.


so far the code isnt working and tells me there is a mistake>


to tell you the truth. i dont think my code will recognize 2000 as a value... i think i need to redesign the whole thing, with different loops. what do you think?
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Dmitri Makovetskiy wrote:

Rene Larsen wrote:Have you tried the code I posted??

This should give you an idea, so you can do what you want.



i am in a direction of this code:



i want to convert all those while loops to for. this would allow me to use if and else.

i must have if and else's.

The structure should be like that.
if
the letter appears in the string
print it


if it doesnt
ask for another number.


so far the code isnt working and tells me there is a mistake>


to tell you the truth. i dont think my code will recognize 2000 as a value... i think i need to redesign the whole thing, with different loops. what do you think?



Again - have you tried my code?? how does it work??
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator



Again - have you tried my code?? how does it work??


Was that because you added public before "class"

we werent taught that. this means i wont be using such codes...


when i tried to compare string with char or char with number, it told me that you cant compare them in a boolean expression..





i added a line in my code

>



[HENRY: added line break to code to help with formatting]
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The code I posted was this one

Now, I don't know WHAT it is you're supposed to learn, but the code above detect legal - and illegal hex numbers.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Rene Larsen wrote:The code I posted was this one

Now, I don't know WHAT it is you're supposed to learn, but the code above detect legal - and illegal hex numbers.



your code is better than mine

but when i write some gibberish (dkaldaldlasldasd)
it tells me the character isnt available, please try again, it does so many times, while some numbers appearing too..

try it yourself
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    Bookmark Topic Watch Topic
  • New Topic