There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
help w/ java pleez.


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
semidevil's Avatar
Senior Member with 178 posts.
 
Join Date: Jun 2002
09-Jul-2002, 01:17 AM #1
help w/ java pleez.
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 1");
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?

Last edited by semidevil : 09-Jul-2002 01:23 AM.
Snake~eyes's Avatar
Senior Member with 639 posts.
 
Join Date: Apr 2002
09-Jul-2002, 09:53 AM #2
that algorithm sounds right but dont you need some if statements to give the letters number values.

Like
if y==V
y=5

i dun know

alos just to nitpick your code

this is an extra line of code that is not needed
int index = 0;
Cause you can define it in your for loop
for(int index = 0; index < str.length(); index++){

i know its not much and you may already know that but I thought it kinda helps... lol

JoyousMonkey's Avatar
Senior Member with 116 posts.
 
Join Date: Mar 2002
Location: England
09-Jul-2002, 12:58 PM #3
Hi, semidevil

It looks to me like your index loop can't get off the ground. You want it to count from "0" to the length of the string, but the string isn't instantiated until the loop starts, so the length is zero.

for (index=0; < str.length; index++)

The string isn't given a value until AFTER this loop, so it counts from zero to zero.

Hope this helps

JoyousMonkey
__________________
takku SAH katta kuiski waastai akias..
..takku SAHas LUni waatkuzi UUL haraatar !
(Ancient Hittite Law)
semidevil's Avatar
Senior Member with 178 posts.
 
Join Date: Jun 2002
09-Jul-2002, 02:52 PM #4
thanx guys, with your suggestions, 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;
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: 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?

edit: edited some stuff....but still not working.

Last edited by semidevil : 09-Jul-2002 03:01 PM.
JoyousMonkey's Avatar
Senior Member with 116 posts.
 
Join Date: Mar 2002
Location: England
09-Jul-2002, 03:15 PM #5
Hi, semidevil.

It's probably bad manners, but I've written a ROUGH program that seems to do what you require. I'm using JDK1.3, so you'll probably have to do a bit of converting...

It's not commented much, but the idea is to store the values for the M,D,C,L,X and I in one array, while the corresponding values 1000,500,100,50,10,1 are in the same positions in another array.

The stdin BufferedReader thing is used to type from the keyboard. There are no awt or swing components. To compile it, just save it into the bin directory of the JDK and then "javac Roman.class" and then "java Roman" will run it from the DOS prompt. It does work, and has a basic error-trapping ability.

I stress it's very very basic!! I've been up since 4am (now 8.20pm) so I can't think much. Brain hurts...

If you want to see it and compare it with what you're doing, it's attached to this post.

Good luck
JoyousMonkey
Attached Files
File Type: txt roman.txt (1.1 KB, 31 views)
__________________
takku SAH katta kuiski waastai akias..
..takku SAHas LUni waatkuzi UUL haraatar !
(Ancient Hittite Law)
semidevil's Avatar
Senior Member with 178 posts.
 
Join Date: Jun 2002
09-Jul-2002, 03:35 PM #6
hey, thanx for the code, but I'm still new to arrays, so I dont know how to use it. and instead of all those if's, I would like to use teh switch statment to make it simplier and more readable....

but yea, go get some sleep.........

I took it down to one error now:

l : variable length
location: class java.lang.String
for (index = 0; index < str.length; index++){
^

I dont know what that means.......

edit: ok, I got this error fixed by putting an () after str.length. but now, when I input a string, that is all I get.......no output whatsoever.....I input a III, and nothing appears........

p,s where should I put my outputBox anyways?? after index++, or inside the loop, or what?

Last edited by semidevil : 09-Jul-2002 03:43 PM.
JoyousMonkey's Avatar
Senior Member with 116 posts.
 
Join Date: Mar 2002
Location: England
10-Jul-2002, 11:29 AM #7
Hello, again.

After getting a little sleep I realised to my horror that I didn't cater for the Roman V (5) in my little program. Worse still, it wouldn't process those Roman numerals where there is a small number in front of a large one...eg IV is 4 and NOT I+V which would create 5.

I've worked out how to do all that, and there is a revised attachment. It is also commented to make things a little more readable.

Semidevil, which version of Java are you using? I'm still using JDK1.3, as I said before. So I am unfamiliar with the output box you mention. Is it similar to a textField? If so, you want to do all the processing and error checking. Once you have obtained your answer you should be outside any loops ( unless the whole program is inside a form of loop). So once the processing is complete you would then display the final answer.

Arrays aren't that difficult. Just think of a street with a name. For example, "eastStreet". Instead of typing "street1=99; street2=100; street3=200..... street99=3" you can set up an array and use a loop. So,

int[] eastStreet=new int[100];

sets up an array called eastStreet with 100 houses on it. Each house has a number, 0 to 99 (NOT 1 to 100). Each house can store an integer. Like mailing a letter, you need the address... to send the number "99" to house 34 on eastStreet you use

eastStreet[34] = 99;

Above all, if things don't work, convert it into English language alogorithms. Draw pictures.. anything. If it doesn't work here it won't work on the computer.
Attached Files
File Type: txt roman.txt (2.2 KB, 31 views)
__________________
takku SAH katta kuiski waastai akias..
..takku SAHas LUni waatkuzi UUL haraatar !
(Ancient Hittite Law)
semidevil's Avatar
Senior Member with 178 posts.
 
Join Date: Jun 2002
12-Jul-2002, 03:46 PM #8
ok.........thanx for the code.

I"ll continue learning java, and see what comes up.....haha
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:47 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.