I have the following lines of code for an assignment. What I don't understand is why, when I uncomment the first few println's before the user prompt in main, the input taken from the function readint seems to capture the first println and not what the user types in. If I leave them all commented, then readint works fine and returns the correct value.

import java.io.*;

class Interface{
    
    static int readint(String str) {
            int     val = 0;
            try {
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);
                
                System.out.print(str);
                String theInput = br.readLine();
                
                val = Integer.parseInt(theInput);
            }
            catch(Exception e) {
                    System.out.println("Error!" + e);
        }
        return val;
    }
    
    public static void main(String[] args){
        
        //System.out.println("What account type do you want to make:");
        //System.out.println("[1]MinBalance");
        //System.out.println("[2]NickelnDime");
        //System.out.println("[3]Gambler");
        int xyz=readint("ENTER NUMBER: ");
        System.out.println("NUM: "+xyz);
    }
    
}

I am using JCreator Pro V4 with JDK 1.6.0.

Try to explain ur problem clearly...so that v can help u easily...what u want to do exactly....

There is nothing wrong with that code , are you shure you didn't made any adjustment on the code?

Alright I'll try again, I want to know why my program displays this output when all the println lines are commented,

ENTER NUMBER: 12
NUM: 12

and why it displays this when one or more of the println lines are not commented?

What account type do you want to make:
ENTER NUMBER: 125
Error!java.lang.NumberFormatException: For input string: "What account type do you want to make:"
NUM: 0

For the second time, I simply uncommented the first println before the user prompt. I am using the exact same code, in fact I just copy and pasted the above code to a new project and I still get the same problem.

hey nothing worng with your code..
Did u use any characters instead of integers

in that case only u will get this kind of error..

if u want to read your input as strings

use this code... it will read everything

import java.io.*;
class Interface
{
static BufferedReader br ;
static int readint(String str)
{
int val = 0;
try
{
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
System.out.print(str);
String theInput = br.readLine();
val = Integer.parseInt(theInput);
}
catch(Exception e)
{
System.out.println("Error!" + e);
}
return val;
}
public static void main(String[] args)
{
System.out.println("What account type do you want to make:");
System.out.println("[1]MinBalance");
System.out.println("[2]NickelnDime");
System.out.println("[3]Gambler");
System.out.println("Enter:");
//int xyz=readint("ENTER NUMBER: ");
try{
InputStreamReader is = new InputStreamReader(System.in);
br = new BufferedReader(is);
String xyz = br.readLine();

System.out.println("NUM: "+xyz);
}catch(Exception e){}

}
}

I tried to run your code, but I get this error.

Exception in thread "main" java.lang.NoClassDefFoundError: ReadFunctions
    at main.main(main.java:3)

I think I figured out what the problem is, but not how to fix it, the code below works fine and if I type in an int I get no errors.

class Interface{
    public static void main(String[] args){
        System.out.print("What account type do you want to make[1]MinBalance[2]NickelnDime[3]Gambler");
        ReadFunctions rd=new ReadFunctions();
        int xyz=rd.readint("ENTER NUMBER: ");
        System.out.println("NUMBER: "+xyz);
    }
}


But the code below doesn't work.

class Interface{
    public static void main(String[] args){
        System.out.print("What account type do you want to make\n[1]MinBalance\n[2]NickelnDime\n[3]Gambler");
        ReadFunctions rd=new ReadFunctions();
        int xyz=rd.readint("ENTER NUMBER: ");
        System.out.println("NUMBER: "+xyz);
    }
}


I get NumberFormatException for input "What account type do you want to make" even though I didn't enter that. Here's the code for ReadFunctions.

import java.io.*;
class ReadFunctions{
    static int readint(String str){
        int     val = 0;
        try{
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            System.out.print(str);
            String theInput = br.readLine();
            val = Integer.parseInt(theInput);
        }
        catch(Exception e){
            System.out.println("Error!" + e);
        }
        return val;
    }
}


It seems that if I do "\n" inside strings or use println instead of print before getting input, the input takes the value of the first string before a line break. How do I possibly solve this? Thanx.

Btw, is this possibly because I have Vista x64? I'm not sure at all.

Hey am using Windows Xp and JDK1.5

May b...the problem is with your platform (OS) which u r using.......am not sure of that.... but in windows platform its working fine with JDK1.5

so u just try in that platform and then come to a conclusion...

I guess I'll have to try to run XP or Linux on VMware to see if Vista x64 is the problem, but I doubt it is. Any other suggestions?

Just a follow-up, I tried the same code with V3LE, V3.5LE, and V4LE, and it works fine. I guess it has something to do with V4Pro. I guess I'll have to use V4LE, I really liked the Pro functions. Too bad I can't use NetBeans 5.5 either as that has the problem too. Stupid Java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.