3 Replies - 2517 Views - Last Post: 08 October 2010 - 11:12 PM Rate Topic: -----

#1 kharenverona  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-October 10

java application that ask the user for starting value and ending value

Posted 08 October 2010 - 10:42 PM

hello..anyone could help me on my laboratory activity in java..my problem is write a java application that ask the user for starting and ending value and then prints all the integer between those 2 values...my code is here:
import java.util.Scanner;
public class exam4
{
public static void main(String[] args)
{
int n, k, result;
Scanner input= new Scanner(System.in);
System.out.print("Enter starting value:");
n=input.nextInt();
System.out.print("Enter ending value:");
k=input.nextInt();
n=0;
k=0;
result=0;
while(n>=k){
n=n+1;;
result=k-1;
System.out.println(result);
}
}
}
whenever i try to run this code,it all sucks..it doesn't print the integers between the 2 values i entered..i don't know what part of the code make it wrong...

This post has been edited by kharenverona: 08 October 2010 - 10:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: java application that ask the user for starting value and ending value

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: java application that ask the user for starting value and ending value

Posted 08 October 2010 - 10:57 PM

Welcome to DIC!

Please use the code tags when posting code, even short pieces.

:code:

If "n" is your first number and it is smaller than "k" then your while() statement should be

while(n <= k){



You don't need result, unless that is part of the assignment. You can simply output "n" and increment it afterwards:

while(n <= k) {
   System.out.println(n);
   n++;
}


This post has been edited by n8wxs: 08 October 2010 - 10:58 PM

Was This Post Helpful? 2
  • +
  • -

#3 aonbyte  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 26
  • Joined: 08-October 10

Re: java application that ask the user for starting value and ending value

Posted 08 October 2010 - 11:06 PM

Scanner input= new Scanner(System.in);
System.out.print("Enter starting value:");
n=input.nextInt();
System.out.print("Enter ending value:");
k=input.nextInt();
n=0;
k=0;


You asked the user for the starting value(n) and the ending value (k). However, after that you wrote two lines that assign the value of 0 to the starting (n) and ending (k) variables. Doing that will overwrite the values you asked for. You can try removing those two lines or moving them to the top like this:

n=0;
k=0;
Scanner input= new Scanner(System.in);
System.out.print("Enter starting value:");
n=input.nextInt();
System.out.print("Enter ending value:");
k=input.nextInt();



EDIT:


I also noticed a minor syntax error.

n=n+1;;


There should only be one semicolon. Like so:

n=n+1;


It is useful to read the compiler error messages you get (if any). A lot of times errors result from minor syntax mistakes like that.

This post has been edited by aonbyte: 08 October 2010 - 11:10 PM

Was This Post Helpful? 1
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: java application that ask the user for starting value and ending value

Posted 08 October 2010 - 11:12 PM

View Postaonbyte, on 08 October 2010 - 10:06 PM, said:

Scanner input= new Scanner(System.in);
System.out.print("Enter starting value:");
n=input.nextInt();
System.out.print("Enter ending value:");
k=input.nextInt();
n=0;
k=0;


You asked the user for the starting value(n) and the ending value (k). However, after that you wrote two lines that assign the value of 0 to the starting (n) and ending (k) variables. Doing that will overwrite the values you asked for. You can try removing those two lines or moving them to the top like this:

n=0;
k=0;
Scanner input= new Scanner(System.in);
System.out.print("Enter starting value:");
n=input.nextInt();
System.out.print("Enter ending value:");
k=input.nextInt();



EDIT:


I also noticed a minor syntax error.

n=n+1;;


There should only be one semicolon. Like so:

n=n+1;


It is useful to read the compiler error messages you get (if any). A lot of times errors result from minor syntax mistakes like that.


Good catch on the placement of n,k initializations! They are unnecessary and should be removed.

This post has been edited by n8wxs: 08 October 2010 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1