Java Mult Array From File - java

I have been having problem trying to get the program to work. The first row in the array is set blank and it keep pushing out the last row in the array.
Scanner console = new Scanner(System.in);
System.out.println("please enter the file name");
String name = console.next();
Scanner input = new Scanner(new File(name));
int length = input.nextInt();
int lengt = input.nextInt();
char[][] array = new char[length][lengt];
for(int i = 0; i < length; i++) {
array[i] = input.nextLine().toCharArray();
}
for(int k = 0; k < array.length; k++){
for(int s = 0; s < array[k].length; s++) {
System.out.print(array[k][s]);
}
System.out.println();
}
input.close();
System.out.println();

Try the adding the line input.nextLine(); to your code like so:
int length = input.nextInt();
int lengt = input.nextInt();
input.nextLine();
char[][] array = new char[length][lengt];
I believe what is happening is you are reading the int's to get length and lengt, but you are leaving the rest of that line. When you call array[i] = input.nextLine().toCharArray(); the first time you are pushing the rest or the line with the length and lengt int's. Since that is counted as the first line in your for loop you are not iterating over the last line.

Related

asking for a name of each customer with array size in for loop

I'm a beginner and I've been really stuck on this problem. I'm not really sure how I can display the number of customer(in an arraySize) (customer #1, customer #2...) and ask for their name in a for loop.
String [] dinerArray;
//initialize our array
dinerArray = new String[arraySize];
for (int i = 1; i == arraySize; i++)
{
System.out.println("enter the name of customer#" + arraySize + ": "
}
I've tried dinerArray.length and then i, arraySize with i, (i=0;i<=arraySize;i++) with arraySize/i.. but nothing seems to work. It would either only print once with customer#0 or print nothing at all
You need to change arraySize to i. That way it will produce customer#1,customer#2 etc....
String [] dinerArray;
//initialize our array
dinerArray = new String[arraySize];
for (int i = 1; i == arraySize; i++)
{
System.out.println("enter the name of customer#" + i+ ": ");
}
If you just want to read them from the console, you can use a Scanner:
Scanner input = new Scanner(System.in);
int arraySize = 10;
String[] dinerArray = new String[arraySize];
for(int i = 0; i < arraySize; ++i)
{
System.out.print("Enter the name of customer#" + (i + 1) + ": ");
dinerArray[i] = input.nextLine();
}
input.close();
im no Java dev but this is pretty basic.
The problem is that your for loop is just running if i is equal to arraySize, but this never happen.
So, try to change your '==' to a '<='
My solution:
Scanner scan = new Scanner(System.in);
int arraySize = 5;
String[] dinerArray = new String[arraySize];
for(int i = 1; i <= dinerArray.length; i++) {
ystem.out.print("pleas enter name for customer#" + i);
dinerArray[i - 1] = scan.nextLine();
}
Im not sure about the scanner think.

Java exception error for input string

I am a beginner in Java programming. I am trying to write a simple program to take size of input followed by list of numbers separated by spaces to compute the sum.
The first input is getting in fine for the second one system shows error as it is trying to parse a blank string into integer. Can you please help with the mistake I am making?
import java.util.Scanner;
public class InputStringforarray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(" Enter size of input ");
int num = scan.nextInt();
System.out.println("Enter data separated by spaces: ");
String line = scan.nextLine();
String[] str = line.split(" ");
int[] A = new int[num];
int sum = 0;
for (int i = 0; i < num; i++)
A[i] =Integer.parseInt(str[i]);
for (int i = 0; i < num; i++)
sum = sum + A[i];
System.out.println("Sum is " + sum);
}
}
The reason you get an exception in your code is because int num = scan.nextInt(); does not process the newline character after the number.
So when the statement String line = scan.nextLine(); is used, it processes the newline character and hence you get an empty string ""
You can either fetch the entire line and parse it to Integer, like this:
int num = Integer.parseInt(scan.nextLine());
or you can go with using nextInt() and then use a blank scan.nextLine() to process the new line after the number, like this:
int num = scan.nextInt();
scan.nextLine();
Your Program has only one error that you were making only one scan object of scanner class, you have to make two scanner class object one will help in getting array size while another will help in getting array element.
import java.util.Scanner;
public class InputStringforarray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in); // change 1
System.out.print(" Enter size of input ");
int num = scan.nextInt();`enter code here`
System.out.println("Enter data separated by spaces: ");
String line = scan1.nextLine();// change 2
String[] str = line.split(" ");
int[] A = new int[num];
int sum = 0;
for (int i = 0; i < num; i++)
A[i] =Integer.parseInt(str[i]);
for (int i = 0; i < num; i++)
sum = sum + A[i];
System.out.println("Sum is " + sum);
}
}

Enter multiple numbers in console

So i have the following chuckj of code and what i want it do is for the user to enter 5 numbers in the netbeans console.The way i want to do it is he either can enter some by being separting the numbers with a space or line by line for exmaple like the following
But what i want is to "Enter a string of numbers" to be only visiable when it is needed to, for example when he needs to enter the next number. Is it possible to do this?
The following is done by the following code
Scanner reader = new Scanner(System.in);
int number;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < 5; i++) {
System.out.println("Enter a string of numbers");
number = reader.nextInt();
numbers.add(number);
}
System.out.println("size" + numbers.size());
for(int takenNumber : numbers){
System.out.println(takenNumber);
}
You could do something like this:
Scanner reader = new Scanner(System.in);
List<Integer> numbers = new ArrayList<>();
int numbersEntered = 0; // Stores how many numbers the user has entered so far
while (numbersEntered < 5) { // Keep asking if they have not entered 5 numbers yet
System.out.println("Enter a string of numbers");
String[] input = reader.nextLine().split(" "); // Split the input by spaces into an array
numbersEntered += input.length; // Add the number of numbers they entered to the variable
for (int i = 0; i < input.length; i++) {
if (numbers.size() < 5) { // Only add the numbers if the list is not full
numbers.add(Integer.parseInt(input[i])); // Add each number they entered to the numbers list
}
}
}
You can use the hasNext() function of Scanner to keep reading while there is more input without doing another prompt. Try:
Scanner reader = new Scanner(System.in);
int number;
ArrayList<Integer> numbers = new ArrayList<Integer>();
int i = 0;
while((i < 5))
{
System.out.println("Enter a string of numbers:");
if(reader.hasNextLine())
{
String[] numbersRead = reader.nextLine().split("\\s");
System.out.print("Read:");
for(int n = 0; n < numbersRead.length; n++)
{
numbers.add(Integer.parseInt(numbersRead[n]));
i++;
}
System.out.println();
}
}

Why an empty space is stored in the first cell of Array?

Can some body explain why an empty space is stored in first cell of my String Array? I am trying to store each line inputted through console into a String array.
import java.util.Scanner;
public class ClassNameHere {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int numberOfLines = in.nextInt();
String[] lines = new String[numberOfLines];
for( int i = 0; i < numberOfLines; i++ ){
lines[i] = in.nextLine();
}
for( int i = 0; i < numberOfLines; i++ ){
System.out.println(lines[i]);
}
}
}
After you call in.nextInt(), the first call to in.nextLine() returns the rest of that same line, and therefore it's empty.
You should add a call to in.nextLine() before the loop to consume that empty line.
int numberOfLines = in.nextInt();
String[] lines = new String[numberOfLines];
in.nextLine(); // add this
for( int i = 0; i < numberOfLines; i++ ){
lines[i] = in.nextLine();
}
for( int i = 0; i < numberOfLines; i++ ){
System.out.println(lines[i]);
}
in.nextInt() reads only the int value not the newline character after that. So that newline will be read by your in.nextLine() so your first index is empty. To eliminate this problem add a dummy in.nextLine() after reading the int.
int numberOfLines = in.nextInt();
in.nextLine();
Another method is to use nextLine() instead of nextInt() to read the and parse the input to int.
int numberOfLines = Integer.parseInt(in.nextLine());
Below code will work for you.
int numberOfLines = in.nextInt();
in.nextLine(); //Add this.
in.nextInt(); reads the integer value and then you click enter which is read by in.nextLine();
The relevant code is
// this reads a number, not the new line after the number
int numberOfLines = in.nextInt();
// reads the rest of the first line you typed.
lines[i] = in.nextLine();
If you want to ignore the rest of the line after the number, add in.nextLine();
You can use nextLine() instead of int numberOfLines = in.nextInt();
I guess it's because method nextInt() reads only the number (3) and the newline that follows is picked up by the first call to nextLine().

Java using a while loop to load user input into an array

I was wondering how to load up an array (with user input) using a while loop. The code below prints a 0.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = 0;
int n = 0;
int[] myArray = new int[10];
System.out.printf("enter a value>>");
while (scan.nextInt() > 0) {
for (i = 0; i > 0; i++) {
myArray[i] = scan.nextInt();
}
System.out.printf("enter a value>>");
}
System.out.printf("array index 2 is %d", myArray[2]);
}
There are multiple things wrong with your code:
First of all
while(scan.nextInt() > 0){
Scanner.nextInt() returns an int from your standard input so you actually have to pick up that value. You are checking here what the user typed but then not using it at all and storing the next thing that the user types by saying:
myArray[i] = scan.nextInt();
You don't really need the outer while loop, just use the for loop, its enough.
However, your for loop is off as well:
for(i = 0; i > 0; i++){
It starts at i equal to 0 and runs while i is greater than 0. This means it will never actually run the code within the loop because 0 is never greater than 0. And if it did run (you started it at some number < 0), you would end up in an infinite loop because your condition i > 0 is always true for positive numbers.
Change the loop to:
for(i = 0; i < 10; i++){
Now, your loop could look like:
for(i = 0; i < 10; i++){ // do this 10 times
System.out.printf("enter a value>>"); // print a statement to the screen
myArray[i] = scan.nextInt(); // read an integer from the user and store it into the array
}
one other way to do it
Scanner scan = new Scanner(System.in);
List list = new ArrayList();
while(true){
System.out.println("Enter a value to store in list");
list.add(scan.nextInt());
System.out.println("Enter more value y to continue or enter n to exit");
Scanner s = new Scanner(System.in);
String ans = s.nextLine();
if(ans.equals("n"))
break;
}
System.out.println(list);
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int[] arr=new int[4];
int i;
for(i=0;i<4;i++)
{
System.out.println("Enter the number: ");
arr[i]=input.nextInt();
}
for(i=0;i<4;i++)
{
System.out.println(arr[i]);
}
}
Hope this code helps.

Categories

Resources