tips on this programming job......

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
I'm trying to write this program, involving loops, but for soem reason, it will not work out. here is what I have so far

import javabook.*;

class Project1
{

public static void main (String [] args)
{
MainWindow mainWindow = new MainWindow ("Project 2");
InputBox inputBox = new InputBox( mainWindow );
OutputBox outputBox = new OutputBox(mainWindow);

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 I = 1;


int y = 0;
int x = 0;

int totalValue = 0;
int index = 0;

String str = new String();
int numberOfLetters = str.length();


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

y = str.charAt(index);
x = str.charAt(index + 1);


if (y > x)
{
totalValue = y + x;
}

else
{
totalValue = y - x;
y++;
}
outputBox.printLine ("blah");
}
}

}

yup yup. I need to write a program that basically asks you to enter roman numberals, and then, convert it to numbers. I tink I got the loop right....right?? but for soem reason, my inputbox to ask for a string wont appear.....any ideas?? according to me, this should work out, but I dont know.

In my head, this is what should happen: it will ask for a string. I enter a string. It will then check the string, starting at index 0, and assign it to y. then, it will check the next index(index + 1), and assign that to x. AFter that, if y > x, it will add. else, y<x, it will subtract( i.e VI = 6, but IV = 4). then, it will increment index++, and go through the loop all over, until index < stringlenght.So that is what's gonig on in my head.........am I even right?? close?
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
ok, update, for some odd reason, my input box came up.....but instead of "enter a string," it says "invalid data. Please try again." hmmm.......dont know....

yes, it's late at night, I'm a bad speller at night.....
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
base 10? um....I dont know. just your basic stuff....i.e

M = 1000;
D = 500;
C = 100;
L = 50;
X = 10;
I = 1;

so lets say if somoen inputs XII. the output should automatically say 12.

btw, the way I did it, is it even right? I declared all those letters as constants. So for example, if the letteer M is at index 0, and it checks index 0, will it realy read it as 1000?? or will it mess up and blow in my face??
 

DanFungus

Diamond Member
Jul 27, 2001
5,857
0
0
yes, base 10....(i.e. 0,1,2,3,4,5,6,7,8,9)
binary, a.k.a is base 2 = 0,1 (two number total, 0 and 1)
dunno other than saying that :p
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
good, atleast I got the numbers right,....now if I can fix my other problems........

when I enter a string into my inputbox, I get no output...........
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
I think your problem is that you declaire X and Y as Integers, but you then try to put character values into them. I'm not sure if you can do that. Also, in your for loop, you have "index < str.length(); " and both are 0 because you give str data in the for loop that you don't go through because index=0=str.length(). You don't need this line of code "int numberOfLetters = str.length();" because you don't use numberOfLetters, you've been using str.length(). That's about all I can see right now. Post some of your new code. I just learned java last year so I like to read code.
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
str = inputBox.getString("Enter a string");

Move this to before your loop.
 

ys

Senior member
Oct 10, 1999
757
0
0
Originally posted by: Semidevil
good, atleast I got the numbers right,....now if I can fix my other problems........

when I enter a string into my inputbox, I get no output...........

you forgot

V = 5;

 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
Originally posted by: JetBlack69
I think your problem is that you declaire X and Y as Integers, but you then try to put character values into them. I'm not sure if you can do that. Also, in your for loop, you have "index < str.length(); " and both are 0 because you give str data in the for loop that you don't go through because index=0=str.length(). You don't need this line of code "int numberOfLetters = str.length();" because you don't use numberOfLetters, you've been using str.length(). That's about all I can see right now. Post some of your new code. I just learned java last year so I like to read code.


that's what Im confused about. we all know that in roman numerals, x = 10, V = 5, etc etc etc. So if I enter XV, and I put in String.charAt(index), it is supposed to search in index 0, which is where the X is. I'm looking inside a character, but what I want is the number(which I declared as a constant, x = 10)........what will come out? any idea? any idea to polish this up?


 

bunker

Lifer
Apr 23, 2001
10,572
0
71
You can try something like this:

You can use totalValue

Loop through the input

Read the first character and run a case statement

case I
totalValue = totalValue + 1

case V
totalValue = totalValue + 5

case X
totalValue = totalValue + 10

etc.....

Then keep looping as long as there are characters in the input.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
ok, here is what I came up w/:

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;
index = 0;
String str;
int totalValue = 0;

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

switch (y){

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

index++

I thought a switch statement might help...but now, it's throwin 10 errors at me, so I dont know.......see anything wrong w/ that code???

you think it looks right?
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
does it run at all or are you getting the errors when you compile?

If it runs, put a system.out.println(y), after y = str.charAt(index); and see what comes up.

Also, I don't see a } after your last break in the switch.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
l : variable length
location: class java.lang.String
for (index = 0; index < str.length; index++){

this is the only error which I have......and I have no idea what it means.....

ps: that little star thing is pointing at teh . on str.length.
^
 

ZaneNBK

Golden Member
Sep 14, 2000
1,674
0
76
You're using inputBox.getInteger() instead of inputBox.getString()

You have no closing brackets on your for loop or your switch statement.

In your loop you should define it as:
index = 0; index < str.length; index++)
instead of:
index = 0; index <= str.length; index++)
Because the index is 0 based but the length starts at 1 (basically). So if your string is 1 character long you access the first character as str[0].. With your code it would try and access str[0] and then str[1] and you'd probably get a runtime error in java. (Don't do a ton of work with java)

Where you're doing "case I", "case V", etc... it's thinking I and V are variables that are undefined. You need int values for those characters there. Look them up in an ASCII chart and put the numeric values there.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
ok, here is the new and improved code guys:

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(y);


switch (y){

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


index++;
}


}}

everything works fine........but when it asks me to input a string, I tried III. when I did, I didnt' get any output at all...............I put an input, and no output.......

thanx guys.
 

ZaneNBK

Golden Member
Sep 14, 2000
1,674
0
76
Get rid of the extra semi-colon at the end of the for line. There should be no semi-colon between the for declaration and the opening bracket of the for block.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
hmmm.........ok, that little ; fixed it, but now, another problem(*sigh*). When I entered 'X' it gave me an output of 88. According to the Ascci table, that is correct, but it needs to be 10. I already declared X as = to 10, so why didn't it do it?

I tried to put a == in my switch staement, but that just made errors......

I did Case X: y == y + 10;

but it didn't like it............

bear w/ me here guys........

please....
 

BigJohnKC

Platinum Member
Aug 15, 2001
2,448
1
0
These classes: MainWindow, InputBox, OutputBox - I'm assuming that these are proprietary classes from your imported package javabook that represent some swing or awt windows, correct? The way that java handles output to these boxes may be the culprit for your lack of output. Just a thought. Unless you're required to output things to a GUI window, I'd use an InputStreamReader to get you keyboard inout, and a System.out.println() for your output.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string");
String str = br.readLine();
etc...
Then at the end, before closing the main():
System.out.println("Final result: " + y);
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
ok, yea, I"m used to the classes from my package, so I think I"d better stick w/ those. Infact, those were the only ones that I know how to use............

but since I got an output, you can see that I have another problem(see my last post) :(
 

vi edit

Elite Member
Super Moderator
Oct 28, 1999
62,484
8,345
126
It's posts like this that remind me why I sucked at programming :D
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
In your switch statement don't use y=y+1....use totalValue = totalValue +1

IGNORE THIS PART>>I'M NOT THINKING STRAIGHT :)
y is a string that represents the character not an int.

Get rid of all the "final int" declarations at the top, you don't need them.


Look at totalValue as your result.