Solved

Java problem with Scanner objects...

Posted on 2009-04-02
3
365 Views
Last Modified: 2013-12-29
Hi there;

I am taking Brand, Type and Price as input with Scanner class but the problem is the program skips input...

I used debugging feature, no use...I am guessing the problem is from the price which is double...so i created some other Scanner objects...but no joy...Please run the attached code...

Best regards...

package hardware;
 

import java.util.ArrayList;

import java.util.Scanner;
 

public class Hardware {
 

    public static void main(String[] args) {

    String brand;

    String type;

    String wireType;

    double price;

    

    ArrayList a1 = new ArrayList();
 

    Scanner sc1 = new Scanner(System.in);

    Scanner sc2 = new Scanner(System.in);

    Scanner sc3 = new Scanner(System.in);
 

    // Enter the Information of MONITOR

    System.out.println("\n***Enter the Info of MONITOR*** ");

    System.out.println("Enter Brand: ");

    brand = sc1.next();
 

    System.out.println("Enter Type (LCD/CRT):");

    type = sc1.next();
 

    sc1.close();

    System.out.println("Enter Price:");

    price = sc2.nextDouble();

    

    sc2.close();

    a1.add(new Monitor(brand, type, price));
 

    // Enter the Information of MOUSE

    System.out.println("\n***Enter the Info of MOUSE*** ");

    System.out.println("Enter Brand:");

    brand = sc3.next();
 

    System.out.println("Enter Type(OPTIC/LASER):");

    type = sc3.next();
 

    System.out.println("Enter WireType (WIRED/WIRELESS): ");

    wireType = sc3.next();
 

    a1.add(new Mouse(brand, type, wireType));
 

    // Enter the Information of KEYBOARD

    System.out.println("\n***Enter the Info of KEYBOARD*** ");

    System.out.println("Enter Brand: ");

    brand = sc3.next();
 

    System.out.println("Enter Type (BASIC/MULTIMEDIA): ");

    type = sc3.next();

    

    sc3.close();

    a1.add(new Keyboard(brand, type));
 

    int i;

    for(i=0; i<a1.size(); i++)

            System.out.println("\nObject "+i+a1.get(i));

    }
 

}

Open in new window

0
Comment
Question by:jazzIIIlove
3 Comments
 
LVL 15

Accepted Solution

by:
fsze88 earned 300 total points
ID: 24049830
sorry, I am no Monitor, Mouse, Keyboard class. unable to debug for you...
could you try to use one Scanner only?
it works fine when I use one Scanner
 

import java.util.ArrayList;

import java.util.Scanner;

 

public class Hardware {

 

    public static void main(String[] args) {

    String brand;

    String type;

    String wireType;

    double price;

    

//    ArrayList a1 = new ArrayList();

 

    Scanner sc1 = new Scanner(System.in);

//    Scanner sc2 = new Scanner(System.in);

//    Scanner sc3 = new Scanner(System.in);

 

    // Enter the Information of MONITOR

    System.out.println("\n***Enter the Info of MONITOR*** ");

    System.out.println("Enter Brand: ");

    brand = sc1.next();

 

    System.out.println("Enter Type (LCD/CRT):");

    type = sc1.next();

 

//    sc1.close();

    

    

    System.out.println("Enter Price:");

    price = sc1.nextDouble();

    

//    sc2.close();

//    a1.add(new Monitor(brand, type, price));

 

 

 

    // Enter the Information of MOUSE

    System.out.println("\n***Enter the Info of MOUSE*** ");

    System.out.println("Enter Brand:");

    brand = sc1.next();

 

    System.out.println("Enter Type(OPTIC/LASER):");

    type = sc1.next();

 

    System.out.println("Enter WireType (WIRED/WIRELESS): ");

    wireType = sc1.next();

 

//    a1.add(new Mouse(brand, type, wireType));

 

 

 

 

    // Enter the Information of KEYBOARD

    System.out.println("\n***Enter the Info of KEYBOARD*** ");

    System.out.println("Enter Brand: ");

    brand = sc1.next();

 

    System.out.println("Enter Type (BASIC/MULTIMEDIA): ");

    type = sc1.next();

    sc1.close();

//    sc3.close();

//    a1.add(new Keyboard(brand, type));

/* 

    int i;

    for(i=0; i<a1.size(); i++)

            System.out.println("\nObject "+i+a1.get(i));

    }

*/ 

  }

}

Open in new window

0
 
LVL 9

Assisted Solution

by:brunoguimaraes
brunoguimaraes earned 200 total points
ID: 24049901
As fsze88 said, it works with only one Scanner object. I created the missing classes Mouse, Monitor and Keyboard and ran this code with one Scanner and it worked.
package hardware;
 

import java.util.ArrayList;

import java.util.Scanner;
 

public class Hardware {
 

	public static void main(String[] args) {

		String brand;

		String type;

		String wireType;

		double price;
 

		ArrayList a1 = new ArrayList();
 

		Scanner sc1 = new Scanner(System.in);
 

		// Enter the Information of MONITOR

		System.out.println("\n***Enter the Info of MONITOR*** ");

		System.out.println("Enter Brand: ");

		brand = sc1.next();
 

		System.out.println("Enter Type (LCD/CRT):");

		type = sc1.next();
 

		System.out.println("Enter Price:");

		price = sc1.nextDouble();
 

		a1.add(new Monitor(brand, type, price));
 

		// Enter the Information of MOUSE

		System.out.println("\n***Enter the Info of MOUSE*** ");

		System.out.println("Enter Brand:");

		brand = sc1.next();
 

		System.out.println("Enter Type(OPTIC/LASER):");

		type = sc1.next();
 

		System.out.println("Enter WireType (WIRED/WIRELESS): ");

		wireType = sc1.next();
 

		a1.add(new Mouse(brand, type, wireType));
 

		// Enter the Information of KEYBOARD

		System.out.println("\n***Enter the Info of KEYBOARD*** ");

		System.out.println("Enter Brand: ");

		brand = sc1.next();
 

		System.out.println("Enter Type (BASIC/MULTIMEDIA): ");

		type = sc1.next();
 

		sc1.close();

		a1.add(new Keyboard(brand, type));
 

		int i;

		for (i = 0; i < a1.size(); i++)

			System.out.println("\nObject " + i + a1.get(i));

	}
 

}

Open in new window

0
 
LVL 12

Author Closing Comment

by:jazzIIIlove
ID: 31565774
swift!

The thing is I have stg in my mind that there is a problem with Scanner class while scanning double variables...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
Java array passed to SQL where clause 23 62
micro services spring boot application error 3 143
Java Timer (static) 9 36
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now