Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java code help; need to input two lines from a text file, one string, one int?

i can use either scanner class or bufferedreader. i was going with scanner class, but i can't figure out how to do line by line with the lines being different types. string first, then int. any ideas? some sample code would be great.

Update:

This is just a little bit of my program, but it ran fine before i made the variables input from a text file so i know this portion is the problem:

File inFile = new File("F:\\Compsci 172\\Project 3\\order.txt");

Scanner input = new Scanner(inFile);

String petType = input.nextLine();

String quantityString = input.nextLine();

int quantity = Integer.parseInt(quantityString);

___________________________________________

but if i run it, this is the result:

Exception in thread "main" java.lang.NumberFormatException: For input string: "☐"

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at SaharaPetsInputOutput.main(SaharaPetsInputOutput.java:28)

Update 2:

the order.txt file has

Dog

2

in it. i was thinking that's what the problem is?

4 Answers

Relevance
  • 8 years ago
    Favorite Answer

    nextLine()

    nextInt()

    Have fun.

  • 8 years ago

    String s1 = scanner.nextLine();

    String s2 = scanner.nextLine(); // read the whole line to get rid of the end-of-line characters

    int i = Integer.parseInt(s2); // convert the String to an int yourself.

    Doing it this way is better than using nextInt() because nextLine() will use up the end-of-line characters so that when you read the String next, you won't get the empty String.

  • ?
    Lv 4
    5 years ago

    i could propose use String.trim() then examine whether that's null or String.equals("") // observe no area String.trim() removes all whitespace interior the begining and end which contain enter so a techniques as i comprehend of so if its sparkling each and everything is deleted it and could grow to be an empt String ("") additionally i think of its greater powerful to apply an if then else interior the whilst loop to close the present writter and create a sparkling record/ or author whilst( inputStream.hasNextLine()){ String line = inputStream.nextLine().trim(); if ( line == null || line.equals("") ) { // close the present flow and create a sparkling record and connect the flow output.close(); output = null; // just to make specific the older one isn't referenced anymore output = new fileWriter( "newfilename"); // assuming using a filewriter } else { // write to the cutting-edge record output.writeline( line); } } // end of whilst loop

  • 8 years ago

    For string use input.next(); and for int input.nextInt();

Still have questions? Get your answers by asking now.