tips on this programming job......

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
uh......what now??

you just confused me.........

why don't you edit this for me, I"m scared to mess it up. :)

so is it y = y +1, or is it totalValue = totalValue + 1


final int M = 1000;
final int D = 500;
final int C = 100;
final int L = 50;
final int X = 10;
final int V = 5;
final int I = 1;


int y = 0;
int index = 0;

int totalValue = 0;
String str;

str = inputBox.getString("Enter a string");
for (index = 0;index < str.length(); index++){
y = str.charAt(index);
outputBox.printLine(y);
if ( y = I){
totalValue = totalValue + 1;
}
break;
else if (y = V){
totalValue = totalValue + 5;
}
else if (y = X){
totalValue = totalValue + 10;
}
else if ( y = C){
totalValue = totalValue + 50;
}
index++;
}}}
 

BigJohnKC

Platinum Member
Aug 15, 2001
2,448
1
0
Originally posted by: bunker
In your switch statement don't use y=y+1....use totalValue = totalValue +1

Look at totalValue as your result.

Yeah, he's right, I hadn't noticed that before. totalValue is the one you want to be incrementing, y is the ascii value of the character. When you're switching on y, you are switching on the acsii value of the char primitive. So say you switch on 'V', then it is a switch on the integer 86, then you're adding 5 to it, so it becomes 91, then you go back to the beginning of the for loop and setting y to something else. Use totalValue to store the total value. But then you need more logic in your switch to indicate whether to subtract or add the value. If you simply switch out totalValue for y right now, your program will output, for example, 6 for both VI and IV. You have a bit more work to do.
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
Your original code should work fine by changing the switch statement:

mainWindow.setVisible (true);
outputBox.setVisible (true);

final int M = 1000;
final int D = 500;
final int C = 100;
final int L = 50;
final int X = 10;
final int V = 5;
final int I = 1;


int y = 0;
int index = 0;
String str;
int totalValue = 0;

str = inputBox.getString("Enter a string");
for (index = 0;index < str.length(); index++);{
y = str.charAt(index);
outputBox.printLine(totalValue);


switch (y){

case I: totalValue = totalValue + 1;
break;
case V: totalValue = totalValue + 5;
break;
case X: totalValue = totalValue + 10;
break;
case C: totalValue = totalValue + 100;
break;
case L: totalValue = totalValue + 50;
break;
case M: totalValue = totalValue + 1000;
break;
}


}


}}


Sorry about confusing the hell out of you! I was confused myself there for a minute!

EDIT -- I had to edit the output box to show totalValue.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
yea, I know.......theres taht subtraction problem...but I figured that there is no reason to think about the subtraction thing, when I cant even get it to add right.......:(

but ok, in my switch, I replaced all the y's w/ totalValues.........I"m still getting the ascii answers....oh, and it's not looping..........:(

grrr..............

my head hurts..............
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
I just edited my post above...see my comment at the bottom.

You have to change the output box to show totalValue.

Also, you have index++ in two spots..get rid of the one at the end of the switch.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
ok, I"m double posting like crazy because I can't seem to find the edit button on posts.......but ok....thanx, bunker.

so I did an input of V, but I get no output.........what gives NOW???!?!

my head still hurts......
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
Post the code from your outputBox class so I can see what it's suppose to accept.
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
Also, at the end of your switch statement put in a

System.out.println("totalValue = " + totalValue);

This is just so we can see if it's adding correctly or not without using your outputBox
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
uh......what now? talking to me? haha

yea, did I say, I'm very very very veyr veyr new in programming, so what you just said was foreign to me.....

post my code from my outputBox class??? whaaaaaaat??

I swear, I'm gonna get this done even if it kills me.....
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
ok, I just did outputbox.Print("totalValue" + totalValue);

not even the word "totalValue" in quotes appeared..........so that means something is really wierd w/ my box.......
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
Heh...in your javabook class, there should be a subclass "class OutputBox".

See if you can find that and post it. When you say outputBox.printLine() the stuff that goes in the parenthesis is passed to the OutputBox class. I need to know what OutputBox does with that.
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
I'm heading home now, I'll check this thread in about an hour. Do you have AIM or ICQ or Yahoo msgr? It may be easier to take this discussion there.
 

BD2003

Lifer
Oct 9, 1999
16,815
1
81
What you need to tell us is the error messages the compiler gives you....

A few hints:

Identation is your friend.
And you dont need brackets after an if or else if theres only one statement.
 

MrMilney

Senior member
Aug 12, 2000
678
0
0
I'd like to point out a logic error in your code. Roman numerals are not simply additive like your code assumes. For example:

I = 1
II = 2
III = 3
IV = 4 (not IIII)
V = 5
VI = 6
VII = 7
VIII = 8
IX = 9 (not VIIII)
X = 10

Using your code IV would be incorrectly interpreted as 6 (1 + 5). Two simple rules to follow are:
  1. you cannot use more than 3 of the same letter in a row (that's why III is OK but IIII is not)
  2. if a smaller number appears to the left of a larger number then you subtract the smaller value from the larger
You will need to re-write your parser if you want your code to work properly. Note: I think that some clocks that have roman numerals on their face do in fact use IIII for 4 but this is not the "real" representation.

You can find a good roman numeral tutorial here.

This page has an online converter that you can use to check the results of your program: link.

edit: added links
 

BD2003

Lifer
Oct 9, 1999
16,815
1
81
How does it convert it to numbers? Do you just need one end sum?

If youre just looking for a sum, you might want to pick out the strings like IV or IX first, then deal with the rest.
 

MrMilney

Senior member
Aug 12, 2000
678
0
0
Originally posted by: bunker
Damn you Milney...had to go and make this more difficult didn't you!?! ;)

Sorry, I just thought maybe you'd be interested in getting the correct answer. ;)
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
Ofcourse I want to get it correct, but if Ican't even get it to add............well..........whoops. Yea, I understand the rules and stuff, but I was just leaving that till later......one step at a time guys......

jeez, all of you are making circles inside my head........
 

HelloWorld02

Junior Member
Jul 10, 2002
2
0
0
mainWindow.setVisible (true);
outputBox.setVisible (true);

final int M = 1000;
final int D = 500;
final int C = 100;
final int L = 50;
final int X = 10;
final int V = 5;
final int I = 1;


char y = 0; /********** needs to be char
int sum = 0;
int index = 0;
String str;
int totalValue = 0;

str = inputBox.getString("Enter a string");
for (index = 0;index < str.length(); index++);{
y = str.charAt(index);
outputBox.printLine(y);


switch (y){

case 'i':
case 'I': sum = sum + I;
break;
case 'v':
case 'V': sum = sum + V;
break;
case 'x'
case 'X': sum = sum + X;
break;
case 'c':
case 'C': sum = sum + C;
break;
case 'l':
case 'L': sum = sum + L;
break;
case 'm':
case M: sum = sum + M;
break;
}


index++;
}


}}


There is more logic you will need to add to figure out the value for those IV cases. But this will translate the characters and display the number.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
thanx........

but it's still not doing it........I enter an input, and I get no output....... hmm...
 

BigJohnKC

Platinum Member
Aug 15, 2001
2,448
1
0
Originally posted by: Semidevil
thanx........

but it's still not doing it........I enter an input, and I get no output....... hmm...

Wow, you're still not done??! ;)

Maybe you should make sure you can write a program that will display anything in the OutputBox before you try and tackle this. Either that or learn how to output stuff to a console - trust me, if you continue to program in Java for any length of time, you wil realize how absolutey worthless java GUIs are and you'll wish you learned the basics of java output when you had the chance, which is now. Read up on System.out.println() and...well, basically the whole java.io package. Readers, Writers, Streams, all that good stuff. It'll be useful later.

BD2003:
Identation is your friend.
And you dont need brackets after an if or else if theres only one statement.
FuseTalk automatically takes away the indents when you post. If he wanted to make the code indent on the forums here, he'd have to put the html escape character for space ("&" plus "nbsp;") wherever he has a space already, and that just gets to be a pain to do every time. The second thing is a coding style decision. I personally put brackets after every if statement just because it makes the code look cleaner and easier to read.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
Originally posted by: BigJohnKC
Originally posted by: Semidevil
thanx........

but it's still not doing it........I enter an input, and I get no output....... hmm...

Wow, you're still not done??! ;)

Maybe you should make sure you can write a program that will display anything in the OutputBox before you try and tackle this. Either that or learn how to output stuff to a console - trust me, if you continue to program in Java for any length of time, you wil realize how absolutey worthless java GUIs are and you'll wish you learned the basics of java output when you had the chance, which is now. Read up on System.out.println() and...well, basically the whole java.io package. Readers, Writers, Streams, all that good stuff. It'll be useful later.


.

yea, that's what I don't get........all my other programs output normally. But for soemreason, this wont' do anything at all. dumb program......I throw a program taht requires some form of computations, and it decides to not output results for me...........dang it....

 

BigJohnKC

Platinum Member
Aug 15, 2001
2,448
1
0
Originally posted by: Semidevil
Originally posted by: BigJohnKC
Originally posted by: Semidevil
thanx........

but it's still not doing it........I enter an input, and I get no output....... hmm...

Wow, you're still not done??! ;)

Maybe you should make sure you can write a program that will display anything in the OutputBox before you try and tackle this. Either that or learn how to output stuff to a console - trust me, if you continue to program in Java for any length of time, you wil realize how absolutey worthless java GUIs are and you'll wish you learned the basics of java output when you had the chance, which is now. Read up on System.out.println() and...well, basically the whole java.io package. Readers, Writers, Streams, all that good stuff. It'll be useful later.


.

yea, that's what I don't get........all my other programs output normally. But for soemreason, this wont' do anything at all. dumb program......I throw a program taht requires some form of computations, and it decides to not output results for me...........dang it....

So you want to print the final output in the OutputBox at the end od the program? You should have something like outputBox.printLine("Total Value = " + totalValue); right before the closing brace for the main() method. I'm guessing on that line though, so I'd say go look at one of your old programs to find out exactly how it is that you output stuff to the OutputBox before, and do that. Keep us updated...
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
*sigh*....nope, I tried putting braces everywhere........no luck.
final int M = 1000;
final int D = 500;
final int C = 100;
final int L = 50;
final int X = 10;
final int V = 5;
final int I = 1;


char y = 0;
int sum = 0;
int index = 0;
String str;
int totalValue = 0;

str = inputBox.getString("Enter a string");
for (index = 0;index < str.length(); index++);{
y = str.charAt(index);



switch (y){

case 'i':
case 'I': sum = sum + I;
break;
case 'v':
case 'V': sum = sum + V;
break;
case 'x':
case 'X': sum = sum + X;
break;
case 'c':
case 'C': sum = sum + C;
break;
case 'l':
case 'L': sum = sum + L;
break;
case 'm':
case M: sum = sum + M;
break;
}


index++;

}
outputBox.printLine("Total Value = " + totalValue);


}}

oh, and it's not looping either!!!