Filling and sorting parallel arrays from single user input - java

I have to accept a single user input of a string and an int ten times, separate them at the space into two parallel arrays. I then have to sort them, find average, etc. Everything I have found on parallel arrays has two different inputs for the string and int. How can I separate the single input into the two arrays?
public static void main(String args[]){
//double[] gradeArray = new double[10];
//String[] nameArray = new String[10];
String name = " "; //name substring
String num = " "; //int substring
String s = " "; //input String
int grade = Integer.parseInt(num); //parsing the numerical string to an int
int x = s.indexOf(' '); //index of " " space
name = s.substring(0, x);
num =s.substring(x + 1);
Scanner input = new Scanner(System.in);
int[] gradeArray = new int[10];
String[] nameArray = new String[10];
//looping to gather 10 user inputs
for(int k = 0; k < 10; k++){
System.out.println("Input Student name and grade: ");
s = input.nextLine();
//not sure how to sepearate String s into String name and String num
}
System.out.println("Highest Grade: " + Grades.highestGrade(gradeArray));
System.out.println("Lowest Grade: " + Grades.lowestGrade(gradeArray));
System.out.println("Class Average: " + Grades.classAverage(gradeArray));
for(int i = 0; i < nameArray.length; i++){
System.out.print(nameArray[i] + ", ");
System.out.print(gradeArray[i]);
System.out.println();
// System.out.print(sort());
}

How can I separate the single input into the two arrays?
First, we will use the already declared array of double to store the grades.
double[] gradeArray = new double[10];
Second, we will use the already declared array of String to store the names.
String[] nameArray = new String[10];
Now, going on to the for loop, we can use the String#split() method to separate the name and the grade on the delimiter " " considering that there will be whitespace between the name and grade as you've mentioned.
for(int k = 0; k < 10; k++){
System.out.println("Input Student name and grade: ");
s = input.nextLine();
String[] tempArray = s.split(" ");
nameArray[k] = tempArray[0]; // store name to nameArray
gradeArray[k] = Double.parseDouble(tempArray[1]); // store grade to gradeArray
}

Related

How to get user input x times and print outside of loop

Im creating an easy blood transfusion game. I want to get the names and blood type then print them to the console. My code below does this but I cant print it to the console because the variable is inside the loop. How do I fix this?
for ( int i = 0; i < 26; i++)
{
System.out.println("Type your full
name first: ");
String donor = in.next();
System.out.println("Now type your blood type: ");
String bloodType = in.next();
}
If you will first gather all data and then print it out, you should temporarily store them somewhere. A good choice is string array or ArrayList.
ArrayList<String> names = new ArrayList<>(26); // 26 is the initial capacity
ArrayList<String> bloodTypes = new ArrayList(26);
for (int i = 0; i < 26; i++) {
System.out.println("Type your full name first: ");
String donor = in.next();
names.add(donor);
System.out.println("Now type your blood type: ");
String bloodType = in.next();
bloodTypes.add(bloodType);
}
Now you can iterate over the ArrayLists and print out your stored values:
for (int i = 0; i < names.size(); i++) {
System.out.println("Blood type of " + names.get(i) + " is " + bloodTypes.get(i));
}

Adding the code for ascending order of user input

import java.util.Scanner;
import java.util.Arrays;
public class searchSorting
{
public static void main (String[]args)
{
String line;
Scanner in = new Scanner(System.in);
System.out.print("How many numbers you want to input?: ");
line = in.nextLine();
System.out.print("Input Number 1: " );
line = in.nextLine();
System.out.print("Input Number 2: ");
line = in.nextLine();
System.out.print("Input Number 3: " );
line = in.nextLine();
System.out.print("Input Number 4: ");
line = in.nextLine();
System.out.print("Input Number 5: " );
line = in.nextLine();
}
public static void sortAscending (double[]arr)
{
Arrays.sort(arr);
System.out.printf("Sorted arr[] = %s",
Arrays.toString(arr));
}}
I am stuck on what the code is for putting the what the user inputs in ascending order. I have looked up and tried multiple resources on ascending order but nothing seems to work. I tried:
System.out.print("Input number 1: "+(i+1+":");
to try and add the inputs instead of writing all of them out but i was an unknown variable.
You should use an array to store input and a loop to read input.
System.out.print("How many numbers you want to input?: ");
int num = in.nextInt();
double[] arr = new double[num];
for(int i = 0; i < num; i++){
arr[i] = in.nextDouble();
}
sortAscending(arr);
You have to create an array. Then put the stuff you are reading into the array and after that call your method.
System.out.print("How many numbers you want to input?: ");
int amount = Integer.parseInt(in.nextLine());
double[] values = new double[amount];
for (int i = 0; i < values.length; i++) {
System.out.print("Input Number " + i + ": ");
values[i] = Double.parseDouble(in.nextLine());
}
sortAscending(values);
Note that the name sortAscending is not accurate, it is not just sorting (and then returning the result or in-place), but also printing. So maybe you should rename it to sortAndPrintAscending. Or just sort it and let your main method to the printing.
Or drop it completely, the method does not really serve any purpose as it is just calling Arrays.sort, might as well do that in main:
System.out.print("How many numbers you want to input?: ");
int amount = Integer.parseInt(in.nextLine());
double[] values = new double[amount];
for (int i = 0; i < values.length; i++) {
System.out.print("Input Number " + i + ": ");
values[i] = Double.parseDouble(in.nextLine());
}
Arrays.sort(values);
System.out.println("Sorted: " + Arrays.toString(values));

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.

Two arrays in for loop

How can I use a for loop asking for student[0]'s grade, [1]'s etc? As of now, my for loop is pointless.
public static void main(String[] args) {
//declare grade and student array
int[] grades = new int[5];
String[] students = { "Bill", "Tom", "Mark", "Nic", "Miguel"};
Scanner sc = new Scanner(System.in);
for(int i = 0; i < grades.length; i++) {
grades[i] = i + 1;
//user input of student's grade
System.out.print("Enter grade for " + students[0] + ": ");
grades[0] = sc.nextInt();
System.out.print("Enter grade for " + students[1] + ": ");
grades[1] = sc.nextInt();
}
}
There must be a better way than hardcoding each student and grade array.
You can take the input and assign it in the loop itself.
for(int i = 0; i < grades.length; i++)
{
System.out.print("Enter grade for " + students[i] + ": ");
grades[i] = sc.nextInt();
}
That's all. You do not need to repeat code as that is what the loop will do by accessing each element with the index i.
However, I am not sure what the line grades[i] = i + 1; accomplishes in your code, since you anyways overwrite that value with the one you get as input.
If i am interpreting your question correct, what you want to do is accept grade values from user using for loop? My answer is as below, I have also printed grades which are accepted.
public static void main(String[] args) {
//declare grade and student array
int[] grades = new int[5];
String[] students = { "Bill", "Tom", "Mark", "Nic", "Miguel"};
Scanner sc = new Scanner(System.in);
System.out.println("please enter the grades of students");
for(int i=0; i<students.length; i++){
grades[i]= sc.nextInt();
}
for(int i=0; i<grades.length; i++){
System.out.println(grades[i]);
}
}

Using a scanner to accept String input and storing in a String Array

Can someone help me please. I have done numerous searches but can't find a solution anywhere.
I'm a beginner to Java and currently practicing some code while on a break from college.
I am trying to make a Phonebook program. At the moment I am trying to add a new contact, below is the code i have but i am not sure how to store the information in an array can someone give me some pointers please.
import java.util.Scanner;
public class addContact {
public static void main(String [] args){
//declare arrays
String [] contactName = new String [12];
String [] contactPhone = new String [12];
String [] contactAdd1 = new String [12];
String [] contactAdd2 = new String [12];
//inputs
String name = "";
String phone = "";
String add1 = "";
String add2 = "";
//method of taken input
Scanner input = new Scanner(System.in);
//while name field is empty display prompt etc.
while (name.equals(""))
{
System.out.println("Enter contacts name: ");
name = input.nextLine();
name += contactName[];
}
while (add1.equals(""))
{
System.out.println("Enter contacts addressline1:");
add1 = input.nextLine();
add1 += contactAdd1[];
}
while (add2.equals(""))
{
System.out.println("Enter contacts addressline2:");
add2 = input.nextLine();
add2 += contactAdd2[];
}
while (phone.equals(""))
{
System.out.println("Enter contact phone number: ");
phone = input.nextLine();
phone += contactPhone[];
}
}
}
A cleaner approach would be to create a Person object that contains contactName, contactPhone, etc. Then, use an ArrayList rather then an array to add the new objects. Create a loop that accepts all the fields for each `Person:
while (!done) {
Person person = new Person();
String name = input.nextLine();
person.setContactName(name);
...
myPersonList.add(person);
}
Using the list will remove the need for array bounds checking.
One of the problem with this code is here :
name += contactName[];
This instruction won't insert anything in the array. Instead it will concatenate the current value of the variable name with the string representation of the contactName array.
Instead use this:
contactName[index] = name;
this instruction will store the variable name in the contactName array at the index index.
The second problem you have is that you don't have the variable index.
What you can do is a loop with 12 iterations to fill all your arrays. (and index will be your iteration variable)
//go through this code I have made several changes in it//
import java.util.Scanner;
public class addContact {
public static void main(String [] args){
//declare arrays
String [] contactName = new String [12];
String [] contactPhone = new String [12];
String [] contactAdd1 = new String [12];
String [] contactAdd2 = new String [12];
int i=0;
String name = "0";
String phone = "0";
String add1 = "0";
String add2 = "0";
//method of taken input
Scanner input = new Scanner(System.in);
//while name field is empty display prompt etc.
while (i<11)
{
i++;
System.out.println("Enter contacts name: "+ i);
name = input.nextLine();
name += contactName[i];
}
while (i<12)
{
i++;
System.out.println("Enter contacts addressline1:");
add1 = input.nextLine();
add1 += contactAdd1[i];
}
while (i<12)
{
i++;
System.out.println("Enter contacts addressline2:");
add2 = input.nextLine();
add2 += contactAdd2[i];
}
while (i<12)
{
i++;
System.out.println("Enter contact phone number: ");
phone = input.nextLine();
phone += contactPhone[i];
}
}
}
Would this work better?
import java.util.Scanner;
public class Work {
public static void main(String[] args){
System.out.println("Please enter the following information");
String name = "0";
String num = "0";
String address = "0";
int i = 0;
Scanner input = new Scanner(System.in);
//The Arrays
String [] contactName = new String [7];
String [] contactNum = new String [7];
String [] contactAdd = new String [7];
//I set these as the Array titles
contactName[0] = "Name";
contactNum[0] = "Phone Number";
contactAdd[0] = "Address";
//This asks for the information and builds an Array for each
//i -= i resets i back to 0 so the arrays are not 7,14,21+
while (i < 6){
i++;
System.out.println("Enter contact name." + i);
name = input.nextLine();
contactName[i] = name;
}
i -= i;
while (i < 6){
i++;
System.out.println("Enter contact number." + i);
num = input.nextLine();
contactNum[i] = num;
}
i -= i;
while (i < 6){
i++;
System.out.println("Enter contact address." + i);
num = input.nextLine();
contactAdd[i] = num;
}
//Now lets print out the Arrays
i -= i;
while(i < 6){
i++;
System.out.print( i + " " + contactName[i] + " / " );
}
//These are set to print the array on one line so println will skip a line
System.out.println();
i -= i;
i -= 1;
while(i < 6){
i++;
System.out.print( i + " " + contactNum[i] + " / " );
}
System.out.println();
i -= i;
i -= 1;
while(i < 6){
i++;
System.out.print( i + " " + contactAdd[i] + " / " );
}
System.out.println();
System.out.println("End of program");
}
}
Please correct me if I'm wrong.`
public static void main(String[] args) {
Scanner na = new Scanner(System.in);
System.out.println("Please enter the number of contacts: ");
int num = na.nextInt();
String[] contactName = new String[num];
String[] contactPhone = new String[num];
String[] contactAdd1 = new String[num];
String[] contactAdd2 = new String[num];
Scanner input = new Scanner(System.in);
for (int i = 0; i < num; i++) {
System.out.println("Enter contacts name: " + (i+1));
contactName[i] = input.nextLine();
System.out.println("Enter contacts addressline1: " + (i+1));
contactAdd1[i] = input.nextLine();
System.out.println("Enter contacts addressline2: " + (i+1));
contactAdd2[i] = input.nextLine();
System.out.println("Enter contact phone number: " + (i+1));
contactPhone[i] = input.nextLine();
}
for (int i = 0; i < num; i++) {
System.out.println("Contact Name No." + (i+1) + " is "+contactName[i]);
System.out.println("First Contacts Address No." + (i+1) + " is "+contactAdd1[i]);
System.out.println("Second Contacts Address No." + (i+1) + " is "+contactAdd2[i]);
System.out.println("Contact Phone Number No." + (i+1) + " is "+contactPhone[i]);
}
}
`
There is no use of pointers in java so far. You can create an object from the class and use different classes which are linked with each other and use the functions of every class in main class.

Categories

Resources