2 Replies - 1147 Views - Last Post: 11 September 2008 - 10:54 PM Rate Topic: -----

#1 ayudame   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-September 08

Need Help with basic java program

Posted 11 September 2008 - 07:42 PM

How do you write a java program that asks to input an integer between two given numbers and output the character that corresponds to the integer. So far I have this:
import java.util.*;

public class YesemK_A3
{
 static Scanner console = new Scanner(System.in);
 public static void main(String[]args)
 {
  int num;		 // integer from the user
  char ch;		 // corresponding character



 }
}


This post has been edited by 1lacca: 12 September 2008 - 02:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need Help with basic java program

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: Need Help with basic java program

Posted 11 September 2008 - 07:53 PM

Nowm, you have to also create an array of characters and then prompt the user for input which they input an int (use console.nextInt()) for this and then print out the array item of the index that was entered.

import java.util.Scanner;
class ClassName{
  public static void main(String[] args){
	Scanner input = new Scanner(System.in);
	System.out.println("Please input a number:");
	int choice = input.nextInt();
	/* -- PSUEDO CODE
	 * create an array of charaters
	 * char[] characters = {'a', 'b', 'c', ..... 'z'};
	 * output the character based on used input
	 * System.out.println("You chose: "+characters[choice]);
	 */
  }
}


<edit>
Or, if you wanted to you could just output the number as a character, but that can have some errors. If you decide to do it like that is would look like so:

import java.util.Scanner;
class ClassName{
  public static void main(String[] args){
	Scanner input = new Scanner(System.in);
	System.out.println("Please input a number:");
	int choice = input.nextInt();
	System.out.println("Your character: "+(char)choice);
  }
}


</edit>

Hope that helps.

This post has been edited by BetaWar: 11 September 2008 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Need Help with basic java program

Posted 11 September 2008 - 10:54 PM

Ok, i whipped this up, the thing you will have to do is implement a way of checking that the user's input is within you're specified range...

import java.util.*;


class IntToChar
{

	private int myInt;
	private char myChar;
	
	Scanner console = new Scanner(System.in);

	void run()
	{
		System.out.println("Please Input and Integer between 58 & 70");
		myInt = console.nextInt();

		myChar = (char)myInt; // we make myChar equal to the char cast 
                                                           // of myInt which the user entered

		System.out.println("The Char Equivalent to " +myInt+ " is " +myChar);
	}

	public static void main(String[] args)
	{
		IntToChar myProgramInstance = new IntToChar();
		myProgramInstance.run(); // will run the method above and keep us away from static problems 
		
	}


}



If you have any trouble let me know :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1