Jump to content

Integer.parseInt exiting method?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
ParseInt causes an exit from the while loop. A bit confused as of why?

 while(bool)
                {
                    System.out.println("Enter MAIL FROM Data " + '\n' +
                            "example: MAIL FROM: <student15@cs32aemail.com>"  + '\n');

                    string = inFromUser.readLine();//read from user
                    int_string = Integer.parseInt("string");
                    System.out.println("parseInt passed");
                }

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#2
Physlet

Physlet

    Newbie

  • Members
  • Pip
  • 2 posts
int_string = Integer.parseInt("string");

The variable that you wanna convert to Integer is called string, you write "string" (with quotes, for java thats represent a string, not a variable).

Try this:
int_string = Integer.parseInt(string);

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Actually what i want to do is just convert a subsection of a string to int...The problem is the parse int call exits the getMailFrom method as soon as that line is executed... ill drop the entire code...but u may not be able to run if u dont have an smpt server.


import java.io.*;
import java.net.*;


public class SMPT
 {
    
        public void sendMail()
        {
            try {
                    Socket socket = new Socket("192.168.81.30", 25);

                    BufferedReader in =
                            new BufferedReader (new InputStreamReader(socket.getInputStream()));

                    BufferedReader inFromUser =
                            new BufferedReader(new InputStreamReader(System.in));

                    DataOutputStream out =
                            new DataOutputStream(socket.getOutputStream());

                    sendMessage(out, "HELO hostname");
                    receiveMessage(in);
                    receiveMessage(in);

                    /*Attempting error recovery  in this section */
                    getMailFrom(inFromUser,out,in );
                    System.out.println("exited getMailFrom");
                    /* Resuming error free code */

                   /* sendMessage(out, "MAIL FROM: <student15@cs32aemail.com>");
                    receiveMessage(in);

                    sendMessage(out, "RCPT TO: " + "<student16@cs32aemail.com>");
                     receiveMessage(in); 
                    sendMessage(out, "RCPT TO: " + "<student28@cs32aemail.com>");
                    receiveMessage(in);
                    sendMessage(out, "DATA");
                    receiveMessage(in);
                    sendMessage(out, "Do you like ketchup? " + '\n' + "How about pickles?");
                    sendMessage(out, '\n' + "." );
                    sendMessage(out, "QUIT"); */
                    socket.close();

               }
               catch (Exception e) {
                  System.out.println(e.getMessage());
               }
        }

        public void sendMessage(DataOutputStream out, String string)
        {
               try 
               {
                    out.writeBytes(string + "\n");
                    //out.flush();
                    System.out.println(string);
               }
               catch (Exception e) {
                    System.out.println(e.getMessage());
               }
         }

        public void receiveMessage(BufferedReader in)
        {
            try{        
                     String string = in.readLine();
                     System.out.println(string);
                     
                }catch (Exception e)
                 {
                    System.out.println(e.getMessage());
                 }
        }

        public void getMailFrom(BufferedReader inFromUser, DataOutputStream dos, BufferedReader in)
        {
            boolean bool = true;
            String string = "";
            int int_string;
            try
            {
                while(true)
                {
                    System.out.println("Enter MAIL FROM Data " + '\n' +
                            "example: MAIL FROM: <student15@cs32aemail.com>"  + '\n');

                    string = inFromUser.readLine();//read from user 
                    dos.writeBytes(string + "\n");
                    string = in.readLine();
                    string = string.substring(0, 4);
                    int_string = Integer.parseInt(string); //cant get past this line 
                    System.out.println("my string: " + string);
                }
                

            }catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
            //throw new UnsupportedOperationException("Not yet implemented");
        }
        private boolean analyze(String string)
        {
                return true;           
            
        }
        public static void main(String argv[])
         {
        SMPT smpt = new SMPT();
        smpt.sendMail();
     }
}



Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
Physlet

Physlet

    Newbie

  • Members
  • Pip
  • 2 posts
Try to catch an NumberFormatException and look what contains string value.
Remember that Integer.parseInt() convert an string to Integer. If the string contains at least one non-numeric character, it will throw an Exception (NumberFormatException).

#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Caught it. It seem i was eating up a space. I built it to chop the first 4 chars...only needed 3....Much thanks
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused: