user3479350
user3479350

Reputation: 43

How to initialize an integer from a file

so i am writing a program and i am having trouble in trying to use initialize or declare a integer from a file in order to use it in the program. this is what the program looks like:

import java.util.Scanner;
import java.io.*;

public class WebberProject1
{
 public static void main(String[] args) 
{
Scanner scanner = new Scanner(new File("portlandvip.txt"));
  String fileName = "portlandvip.txt";

while (scanner.hasNext())
{
 String firstName = scanner.next();
 String lastName = scanner.next();

  System.out.println(" " + firstName + " " + lastName);            
   scanner.nextLine();  
}       
    try {
        File file = new File(fileName);

        String a,b,c,d;
        while (scanner.hasNext()) {
            a = scanner.next();
            b = scanner.next();
            c = scanner.next();
            d = scanner.next();
            int num = Integer.parseInt(c);
            System.out.println(num);
        }
        scanner.close();
    } catch (IOException e) {
        e.printStackTrace();
    } 
 }
 }
 import java.util.Scanner;
 import java.io.*;


public class WebberProject1
{

public static void main(String[] args) throws IOException {

Scanner scanner = new Scanner(new File("portlandvip.txt"));


while (scanner.hasNext())
{
String firstName = scanner.next();
String lastName = scanner.next();

System.out.println(" " + firstName + " " + lastName);            
scanner.nextLine();  
}       
    try {
        File file = new File("portland.txt");

        String a,b,c,d;
        while (scanner.hasNext()) {
            a = scanner.next();
            b = scanner.next();
            c = scanner.next();
            d = scanner.next();
            int num = Integer.parseInt(c);
            System.out.println(num);
        }
        scanner.close();
    } catch (IOException e) {
        e.printStackTrace();
    } 
    }
}

This is the file i am using.

Loras Tyrell 5 Club

Margaery Tyrell 8 Box

Roslin Frey 2 Box

Sansa Stark 2 Club

Jon Snow 5 Club

Edmure Tully 3 Box

Joffrey Baratheon 20 Court

Stannis Baratheon 4 Club

Jaime Lannister 2 Box

Cersei Lannister 1 Court

Beric Dondarrion 8 Court

Balon Greyjoy 16 Box

Olenna Tyrell 4 Court

Mace Tyrell 5 Box

Tyrion Lannister 2 Club

Sandor Clegane 2 Court

Gregor Clegane 6 Club

Samwell Tarly 3 Club

Petyr Baelish 6 Court

(i changed the spacing to make it look presentable)

What i am trying to do is to use the number after the last name in order to multiply that number with a certain number depending on the type of seat the data gives (club, court, etc). But, i cant figure out how to isolate the variable and the type of seat to use them for the program.

This is what the program outputs:

Loras Tyrell

Margaery Tyrell

Roslin Frey

Sansa Stark

Jon Snow

Edmure Tully

Joffrey Baratheon

Stannis Baratheon

Jaime Lannister

Cersei Lannister

Beric Dondarrion

Balon Greyjoy

Olenna Tyrell

Mace Tyrell

Tyrion Lannister

Sandor Clegane

Gregor Clegane

Samwell Tarly

Petyr Baelish

Upvotes: 0

Views: 105

Answers (2)

Mohsen Kamrani
Mohsen Kamrani

Reputation: 7457

Try this:

public class WebberProject1 {
    public static void main(String[] args) {
        String fileName = "portlandvip.txtt";

        try {
            File file = new File(fileName);
            Scanner scanner = new Scanner(file);
            String a,b,c,d;
            while (scanner.hasNext()) {
                a = scanner.next();
                b = scanner.next();
                c = scanner.next();
                d = scanner.next();
                int num = Integer.parseInt(c);
                System.out.println(num);
            }
            scanner.close();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}

Upvotes: 0

Akash Thakare
Akash Thakare

Reputation: 22982

Most convinient method I use for this problem is replace all letters except digits to blank.

String s="Tyrion Lannister 2 Club";
int i=Integer.parseInt(s.replaceAll("[\\D]", ""));

this will give you number inside String as I want to tell you it will work only for String like you have.

It will not convenient for String as "Tyrion Lannister 2 Club and Number 44 " it will give you 244.

Upvotes: 1

Related Questions