944,120 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1123
  • Java RSS
Nov 20th, 2007
0

need help with java program.

Expand Post »
hi i have this assingment due very soon and i am totally stumped. I have to use java and im not good with it at all , ive' been searching everywhere and i can't find anything that would help me. Hopefully you guys can help me , thanks in advance.

i am suppose to do this.

Write a program that does the following.

Ask the user how much data they have
For every number entered print the number and whether it was larger or smaller, and by how much, than the previous number.
Compare the first number to 0.

for example, (comparing to first number (0) 10, 10 greater // 20, 10 greater // 50, 30 greater.

this is what i have so far :

{public static void main(String[] args)
{int num1=0,nnum,subvalue,addvalue,sum,counter;
Scanner input = new Scanner(System.in);
System.out.print("how much data do you have? ");
counter=input.nextInt();
counterkb=counter+kb.nextInt();
num1=input.nextInt();
System.out.print("enter a number");
nnum=input.nextInt();
subvalue=num1-nnum;
addvalue=num1+nnum;
for (num1=1;counter<=counter+kb;nnum=num1+1)
{sum=num1+nnum;}
System.out.println("The sum is" +sum);
if(num1>nnum);
{System.out.print(num1 +" is greater than nnum by "+subvalue);
{
if(num1<nnum);
{ System.out.print(nnum +" is less than num1 by "+addvalue);
}
}
}
}
}



i think the program should be really simple. we did not learn any strings or arrays, really basic java. im not sure how i am suppose to output weather the number was greater/less than how many. and i am not sure how i am suppose to set the counter and loop// + what loop i should use. any help would be greatly appreciated. thanks in advance again.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elisa_yong is offline Offline
6 posts
since Nov 2007
Nov 20th, 2007
0

Re: need help with java program.

make sure u are using the scanner class right, remember it reads until the first space is u do scanner.next() and if you do scanner.nextInt() will scan the next set of number until it gets to a space, my advise is to make it simple to work create as many variables as data is gonna be entered now if you havent gone over arrays yet try reading 2 data entries the first time and then do what ever you need to do with them and then throw one away and get the next data on the string using scanner.nextInt()
Reputation Points: 8
Solved Threads: 2
Newbie Poster
uonsin is offline Offline
17 posts
since Oct 2007
Nov 20th, 2007
0

Re: need help with java program.

hello, me again. thanks for the reply appreciate it.
i am getting thise error and i dont know why.



Scanner input = new Scanner(System.in);



testing.java:4: ';' expected
Scanner input = new Scanner(System.in);
^
1 error
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elisa_yong is offline Offline
6 posts
since Nov 2007
Nov 20th, 2007
0

Re: need help with java program.

you might wanna past the whole code and numerate each line and mark where you r getting the error
Reputation Points: 8
Solved Threads: 2
Newbie Poster
uonsin is offline Offline
17 posts
since Oct 2007
Nov 21st, 2007
0

Re: need help with java program.

int num1=0,nnum,subvalue,addvalue,sum,counter;

i forgot on that line i think if you declare a variable you have to put a ';' after and if you dont declar it then you can do ','

int num = 0'
int nnum, subvalue, addvalue, sum, counter;
now i m not sure if that will help because i try to avoid doing it like that because it easier for me to look for errors
Last edited by uonsin; Nov 21st, 2007 at 12:41 am.
Reputation Points: 8
Solved Threads: 2
Newbie Poster
uonsin is offline Offline
17 posts
since Oct 2007
Nov 21st, 2007
0

Re: need help with java program.

okay this is what i have so far...
the program runs but : it does not do what i want it to do . lol

{int oldNumber=0,newNumber,subvalue,addvalue;
Scanner input = new Scanner(System.in);
System.out.print("The first number is " +oldNumber);
do {
System.out.print("Enter a new number: ");
newNumber=input.nextInt();
subvalue=oldNumber-newNumber;
addvalue=newNumber+oldNumber;
if (newNumber > oldNumber)
{ System.out.print("greater by "+subvalue);
} else if (newNumber < oldNumber)
{ System.out.print("less by "+addvalue);
} else
{ System.out.print("don't know yet");
}
} while (oldNumber>=0);

===============================================
RESULTS:

The first number is 0Enter a new number: 2
greater by -2Enter a new number: 5
greater by -5Enter a new number: 2
greater by -2Enter a new number: 1
greater by -1Enter a new number: 2
greater by -2Enter a new number:
===============================================
that is what i am getting. a minus sign in front of the number which i do not want. AND the "less than" and "greater than" does not work. And it wont tell me how much larger or smaller the new value was. ( named the variables "addedvalue" "subvalue" )


pls help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elisa_yong is offline Offline
6 posts
since Nov 2007
Nov 21st, 2007
0

Re: need help with java program.

okay i changed the + and - of the add and sub value. it works now. but i still cannot get the " less than " to work. thanks in advance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elisa_yong is offline Offline
6 posts
since Nov 2007
Nov 21st, 2007
0

Re: need help with java program.

just noticed my program does not compare to previous numbers inputed but compares it solely to the first num. please help need tips so it will compare to previous number.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elisa_yong is offline Offline
6 posts
since Nov 2007
Nov 21st, 2007
0

Re: need help with java program.

to compare the 2nd number with the 3rd number you need to make the variable that equals the 1st number equals to the second number and the variable that equals the 2nd number equal to the new number, that is what i meant by using only 2 variables and then throw the 1st one out in a prin
Reputation Points: 8
Solved Threads: 2
Newbie Poster
uonsin is offline Offline
17 posts
since Oct 2007
Nov 21st, 2007
0

Re: need help with java program.

thanks a lot. i guess i was just not getting the concept of it. appreciated you help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elisa_yong is offline Offline
6 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to download a file without opening file download dialog box
Next Thread in Java Forum Timeline: integer comparison





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC