-2
import java.util.Scanner;

public class test1{
    public static void main(String[] args) {

       java.io.File test2 = new java.io.File("test3.txt");

       try
       {
           Scanner input = new Scanner(test2);
           while (input.hasNext()){
              String num = input.nextLine();
              System.out.println(num);
           }
       }  catch (Exception e){
        System.out.println("could not find file");
       }
    }
}  

I was trying to figure out a way to convert a string into a int. i was inputing a string that looks like 0000100001000010000001100000

Aditya Jain
  • 1,077
  • 1
  • 12
  • 25
henry
  • 1
  • 2
  • 2
  • 4

7 Answers7

2

Scanner class has the feature to convert to another type such as int, double etc

For converting to Integer

Scanner input = new Scanner(test2);
 while(input.hasNext())
 {
  int num = input.nextInt();
  System.out.println(num);
 } 

For converting to Double

 Scanner input = new Scanner(test2);
     while(input.hasNext())
     {
      double num = input.nextDouble();
      System.out.println(num);
     } 
Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
1

You may want to look into Integer.parseInt().

The documentation can be found here- simply pass your String as a parameter, and you'll be returned an int representation.

In your case, you would want to use it like this:

String num = input.nextLine();
int yourValue = Integer.parseInt(num);

Parsing a double would use a similar method- Double.parseDouble() should do the trick.


Alternatively, if you're only planning on using this conversion for the sake of reading in from a file, then using input.nextInt(); instead of input.nextLine(); may suit you well!

username tbd
  • 9,152
  • 1
  • 20
  • 35
1

Take a look at Integer.parseInt()

String num = input.nextLine();
int numInt = Integer.parseInt(num, 2); 
System.out.println(numInt);

If this means binary number, you need to use 2 as second parameter

0000100001000010000001100000

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
1

Every thing has a limit, If your num is 0000100001000010000001100000, then it's not possible to convert it in int. You have to convert it in double or float or reduce your String, trying to convert in int with Integer.parseInt(num) will throw you java.lang.NumberFormatException if you need it in double then do this Double.parseDouble(num) see the Primitive Data Types limits

subodh
  • 6,136
  • 12
  • 51
  • 73
0

What you want to use in your particular case is

Integer.parseInt(num);

If num is your string to convert. Considering you asked to convert to a double too, you will have to convert it with

Double.parseDouble(num);

The only problem is if you test for the int only and the user enters a decimal number, an exception will be thrown.

phadaphunk
  • 12,785
  • 15
  • 73
  • 107
0

try this

Scanner input = new Scanner(test2);
while (input.hasNext())
{
    String num = input.nextLine();
    try
    {
        long l = Long.parse(num);
        System.out.println(l);
    }
    catch(Exception ex)
    {
        System.out.println("Input string is not a number");
    }
}

If your input is a binary number and you want to display it in decimal form the u can use this:

long l = Long.parse(num, 2);
PC.
  • 6,870
  • 5
  • 36
  • 71
0

If you want to convert a valid string to a double one way of doing do is by using

double value=Double.parseDouble("1000.25");
codeMan
  • 5,730
  • 3
  • 27
  • 51