Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: data values help

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy data values help

    hello i am new to java. I don't get why tempNumber only catches 2 digit numbers. and when I choose "2" it doesnt respond first time. When it displays the menu again and I enter again "2" then it works.

    	import java.io.*;
    	//import java.math.*;
    	public class java_prime{
    		//We will create two class arrays, these
    		//Can be used by all the methods in the class
    		static int number[] = new int[100];
    		static String name[] = new String[100];
    		//We will create a counter, this will help us
    		//remember how many names and numbers we are storing
    		static int counter = 0;
    		//Main method
    		public static void main(String[] args) throws IOException{
    			String tempName = null;
    			int tempNumber = 0;
    			//Create a Buffered reader for input, you do remember how don't you?
    			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    			//Here we output our Menu
    			displayMenu();
    			//Here we get user input
    			String temp = in.readLine();
    			//Loop until the use wants to quit
    			while(temp!="3"){
    				if(temp.equals("1")){
    					System.out.print("Enter a new Name: ");
    					tempName = in.readLine();
    					System.out.println();
    					System.out.print("Enter a new Number: ");
    					tempNumber = in.read(); 
    					//Here we call the method to add the record
    					addEntry(tempName, tempNumber);
    				}else if(temp.equals("2")){
    					//In here we call the method to output the records
    					showRecords();
    				}
    				//Display the menu again and ask for input again
    				displayMenu();
    				temp = in.readLine();
    			}
    		}
    		//Here is all the methods you will need!
    		public static void  addEntry(String tempName, int tempNumber){
    			name[counter] = tempName;
    			number[counter] = tempNumber;
    			counter++;
    		}
    		public static void displayMenu(){
    			System.out.println("\n\n\n");
    			System.out.println("WELCOME TO THE JAVA PDA - DESKTOP VERSION\n\nWhat would you like to do today?\n\n1. Enter a new Name and Number\n2. View all names and Numbers\n3. Quit");
    		}
    		public static void showRecords(){
    			int i;
    			System.out.println("Name\t\t\t\tNumber");
    			for(i = 0; i!=counter;i++){
    				System.out.println(name[i]+"\t\t\t\t"+number[i]);
    			}
    		}
    	}
    Last edited by Norm; January 1st, 2013 at 08:54 PM. Reason: added code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: data values help

    Try debugging the code by printing out the value of tempNumber right after it is read in so you can see what is being read.


    Please copy the full contents of the console for when you execute the program and post it here.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: data values help

    what do you mean copy the contents? I use eclipse and java development kit. I pasted my code up there?

    I ran the program and this is what i got for tempNumber
    What would you like to do today?

    1. Enter a new Name and Number
    2. View all names and Numbers
    3. Quit
    1
    Enter a new Name: ok

    Enter a new Number: 123455
    49 - tempNumber

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: data values help

    When you execute the code, it writes to the console and you enter data on the console.
    Can you copy the contents of the console and paste it here so we can see what is printed by the program and what you entered as data to the program? Also add some comments that describe what the problem was with the way the program executed.

    One problem I see is the use of != to compare Strings. You need to use the equals() method to compare the contents of Strings.

    49 is what the read() method returns when it reads '1'. Read the API doc for the method for details.
    To see '1' vs 49, print this: (char)49
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: data values help

    why would/does it reads '1' when I enter 123455.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: data values help

    why would/does it reads '1' when I enter 123455.
    Read the API doc for the read() method. It says that it reads a single character.

    To get an int value you need either to
    read a String and convert it to int using the Integer class's method
    or use the Scanner class to read user input. The Scanner class has methods to read and convert character input to an int value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  2. retrieve data from database using Hashmap (multiple values in one key)
    By ashin12 in forum Collections and Generics
    Replies: 2
    Last Post: April 4th, 2012, 05:22 AM
  3. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 02:48 AM
  4. Character Values Inside of Number Values
    By bgroenks96 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 08:27 PM
  5. Java program to find the minimum and maximum values of input data
    By awake77 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 20th, 2008, 05:12 PM